|
 |
|
สอนการเขียน ajax ตัวนี้หน่อย กำลังศึกษา เขียน framework เองเล่นๆครับ |
|
 |
|
|
 |
 |
|
ไฟล์ s.html
Code (JavaScript)
<a href="ss.html" id="abc">ทดสอบ</a>
<div id="show"></div>
<script type="text/javascript">
document.getElementById("abc").onclick = function() {
document.getElementById("show").innerHTML=window.location.href;
};
</script>
ผมต้องการคลิกที่ทดสอบที่ไฟล์ s.html
แล้วได้ผลลัพธ์ที่ไฟล์ ss.html มาแสดง <div id="show"></div> ของ s.html
|
 |
 |
 |
 |
Date :
2014-01-15 22:39:56 |
By :
weaned |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูลไม่แสดงตรงนี้ครับ <span id="mySpan"></span>
<html>
<head>
<title>Ajax</title>
</head>
<script language="JavaScript">
var HttPRequest = false;
function test() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
HttPRequest.open('POST',url,false);
HttPRequest.send(null);
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
window.onload = test;
document.getElementById("abc").onclick = test;
</script>
<body>
<span id="mySpan"></span>
<a href="ss.php" id="abc">ทดสอบ</a>
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-01-15 23:19:41 |
By :
weaned |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์ s.php
Code (PHP)
<html>
<head>
<title>Ajax</title>
</head>
<script language="JavaScript">
var HttPRequest = false;
function test() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'ss.php';
HttPRequest.open('POST',url,false);
HttPRequest.send(null);
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
//window.onload = test;
//document.getElementById("abc").onclick = test;
</script>
<body>
<span id="mySpan"></span>
<a href="#" id="abc" onClick="test();">ทดสอบ</a>
</body>
</html>
ไฟล์ ss.php
Code (PHP)
<?php echo "TEST XXX"; ?>
|
 |
 |
 |
 |
Date :
2014-01-16 09:26:54 |
By :
WiTT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ สำหรับ NO 3
ที่ผมต้องการคือให้แท็ก a ส่ง url มากกว่า แล้วก็ให้ script onload function onclick เพื่อไม่ให้ตรงลิ้งค์ใส่ onclick ครับ
|
 |
 |
 |
 |
Date :
2014-01-16 09:47:56 |
By :
weaned |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามจุดประสงค์ที่คุณว่า
คำสั่ง ตรง HttPRequest.open('POST',url,false); ค่า url คุณจะเอามาจากไหน ?
ให้ แท็ก a เป็นตัวส่งค่าไปให้หรือ ? มันส่งไปไม่ได้นอกจากจะกำหนดค่าที่ฟังชั่น test หรือ เพิ่ม onclick
หรือไม่คุณก็ต้องใช้ jQuery
|
 |
 |
 |
 |
Date :
2014-01-16 10:13:17 |
By :
WiTT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|