2. json2.js 를 다운로드
3. 다음과 같이 사용
function addEventHandler(obj, eventName, handler) {
if (document.attachEvent) {
obj.attachEvent("on" + eventName, handler);
} else if (document.addEventListener) {
obj.addEventListener(eventName, handler, false);
}
}
function getActivatedObject(e) {
var obj;
if (!e) {
// IE 옛날 버전은 event 객체를 전달하지 않음
obj = window.event.srcElement;
} else if (e.srcElement) {
// IE는 target 이 아니라 srcElement 로 넘어온다.
obj = e.srcElement;
} else {
obj = e.target;
}
return obj;
}
<div id="root">
<div id="firstParentDiv">첫번째 부모 div
<div id="firstChildDiv">첫번째 자식 div</div>
<div id="secondChildDiv">두번째 자식 div</div>
</div>
</div>
function showTree() {
var firstParent = document.getElementById('firstParentDiv');
var root = firstParent.parentNode;
var firstChild = firstParent.firstChild;
while (firstChild.nodeName == "#text") {
// 첫번째 "첫번째 부모 div" 문자열노드, 두번째 div
alert(firstChild.nodeValue);
firstChild = firstChild.nextSibling;
}
var secondChild = firstChild.nextSibling;
while (secondChild.nodeName == "#text") {
// 처음은 리턴 때문에 null 문자열노드, 두번째 div
alert(secondChild.nodeValue);
secondChild = secondChild.nextSibling;
}
secondChild = firstParent.lastChild;
while (secondChild.nodeName == "#text") {
// 처음은 리턴 때문에 null 문자열노드, 두번째 div
alert(secondChild.nodeValue);
secondChild = secondChild.previousSibling;
}
firstChild = secondChild.previousSibling;
while (firstChild.nodeName == "#text") {
// 처음은 리턴 때문에 null 문자열노드, 두번째 div
alert(firstChild.nodeValue);
firstChild = firstChild.previousSibling;
}
}<div id="me"></div>
function getChildNodes() {
var me = document.getElementById('me');
alert (me.childNodes.length);
}function createRequest() {
var request;
try {
request = new XMLHttpRequest();
} catch(tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch(otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
function getDetiales(itemName) {
// var 키워드를 사용하지 않고 전역으로 사용함
request = createRequest();
if (request == null) {
alert("Unable to create request");
}
var url = "ajaxtest2.php?imageId=" + escape(itemName);
request.open("GET", url, true);
request.onreadystatechange = displayDetails;
request.send(null);
}function displayDetails() {
if (request.readyState == 4) {
if (request.status == 200) {
var description = document.getElementById("description");
description.innerHTML = unescape(request.responseText);
}
}
}$con = @new mysqli($GLOBALS['gServerName'], $GLOBALS['gUserId'],
$GLOBALS['gPassword'], $GLOBALS['gDatabase']);
if ($con->connect_errno) {
printf("%d : %s <br/>", $con->connect_errno, $con->connect_error);
exit();
}
$userId = "skanddh";
$sql = "select * from User where userId = " . $userId;
$result = $con->query($sql);
$rowCount = $result->num_rows;
for($i=0; $i<$rowCount; $i++) {
$row = $result->fetch_assoc();
print("userId = " . htmlspecialchars(stripslashes($row['userId'])) .
"<br/>");
print("userName = " . htmlspecialchars(stripslashes($row['userName'])) .
"<br/>");
print("age = " . htmlspecialchars(stripslashes($row['age'])) . "<br/>");
}
for($i=0; $i<$rowCount; $i++) {
$row = $result->fetch_object();
print("userId = " . htmlspecialchars(stripslashes($row->userId)) .
"<br/>");
print("userName = " . htmlspecialchars(stripslashes($row->userName)) .
"<br/>");
print("age = " . htmlspecialchars(stripslashes($row->age)) . "<br/>");
}
$result->free();
$con->close();
$userId = 'me';
$userName = 'john';
$age = 30;
$sql = " insert into User (userId, userName, age) " .
" values ('" . $userId . "', '". $userName . "', ". $age . ") ";
$con->query($sql);
printf("%d user inserted into database.<br/>", $con->affected_rows);
$con->commit();
$sql = " insert into User (userId, userName, age) " .
" values (?, ?, ?) ";
$stmt = $con->prepare($sql);
$stmt->bind_param("ssi", $userId, $userName, $age);
$stmt->execute();
printf("%d user inserted into database.<br/>", $con->affected_rows);
$stmt->close();
$sql = " select userId, userName, age from User ";
if ( $stmt = $con->prepare($sql) ) {
$stmt->execute();
if ($stmt->errno) {
printf("execute() >> %d : %s <br/>",$stmt->errno, $stmt->error);
}
$stmt->bind_result($rUserId, $rUserName, $rAge);
$count = 0;
while ($stmt->fetch()) {
$count++;
print("userId = " . htmlspecialchars(stripslashes($rUserId)) .
"<br/>");
print("userName = " . htmlspecialchars(stripslashes($rUserName)) .
"<br/>");
print("age = " . htmlspecialchars(stripslashes($rAge)) . "<br/>");
}
printf("%d row seleted. <br/>", $count);
$stmt->close();
}
class Point {
private $x;
private $y;
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
public function __get($name) {
return $this->$name;
}
public function __set($name, $value) {
$this->$name = $value;
}
public function display() {
print("\$this->x = " . $this->x . "<br/>");
print("\$this->y = " . $this->y . "<br/>");
}
}
$point = new Point(3, 5);
print($point->x); // 3
print($point->y); // 5
$point->x = 7;
print($point->x); // 7
$point->display();
class Rectangle extends Point {
private $width;
private $height;
public function __construct($x, $y, $width, $height) {
parent::__construct($x, $y);
$this->x = $x;
$this->y = $y;
}
}
$rectangle = new Rectangle(10, 20, 100, 200);
print($rectangle->x); // 10interface Displayable {
public function display();
}
class WebPage implements Displayable {
public function display() {
print("Hello World!! <br/>");
}
}
$page = new WebPage();
$page->display();
class Math {
const pi = 3.14159;
public static function squared($input) {
return $input * $input;
}
}
print("Math::pi = " . Math::pi . "<br/>");
print("Math::squared(8) = " . Math::squared(8) . "<br/>");
abstract class A {
public abstract function operationX($param1, $param2);
}
class ConcreteA extends A {
public function operationX($param1, $param2) {
return $param1 + $param2;
}
}
$a = new ConcreteA();
print($a->operationX(3, 4) . "<br/>");