« Excel VBA 備忘: 將 Excel 當資料庫使用 | 首頁 | 打造自己的 Floppy Linux »

2006年07月21日

Ajax Sample Code


HTML & JavaScript 內容:

<html>
<head>
<script language="javascript">

//取得文件本身的 URL
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0,xend);

function ajax_do(url) {

	//判斷傳入的 URL 開頭是否為 "http"
	if (url.substring(0,4) != 'http') {
		url = base_url + url;
	}

	//建立新的 JavaScript 元件
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;
	
	//在目前的文件中加入剛剛建立的 JS 元件 (執行元件裡的 script)
	document.body.appendChild(jsel);
}

</script>
</head>
<body>
	<div id="contentdiv"></div>
	<input type="button" value="Get content" onclick="ajax_do('filename.php')" />
</body>
</html>

PHP 內容:

<?php
	header("Cache-Control: no-cache");
	$html = "<font color=yellow>" . rand(1,256) . "</font>";
	echo "document.getElementById('contentdiv').innerHTML = '$html';";
?>

測試:

參考資料:

相關網頁:

其他備忘:

Posted by Jamyy at 2006年07月21日 11:07

Trackback Pings

TrackBack URL for this entry:
http://cha.homeip.net/cgi-bin/mt/mt-tb.cgi/215

Comments

ok

Posted by: e at 2008年03月31日 18:13