我正在使用下面的代码,用于加载内容而不加载页面!加载内容而不重新加载页面
的index.html
<html>
<head>
<script type="text/javascript">
// <![CDATA[
document.observe('dom:loaded', function() {
var newsCat = document.getElementsByClassName('newsCat');
for(var i = 0; i < newsCat.length; i++) {
$(newsCat[i].id).onclick = function() {
getCatPage(this.id);
}
}
});
function getCatPage(id) {
var url = 'load-content.php';
var rand = Math.random(9999);
var pars = 'id=' + id + '&rand=' + rand;
var myAjax = new Ajax.Request(url, {
method: 'get',
parameters: pars,
onLoading: showLoad,
onComplete: showResponse
});
}
function showLoad() {
$('newsContent').style.display = 'none';
$('newsLoading').style.display = 'block';
}
function showResponse(originalRequest) {
var newData = originalRequest.responseText;
$('newsLoading').style.display = 'none';
$('newsContent').style.display = 'block';
$('newsContent').innerHTML = newData;
}
// ]]>
</script>
</head>
<body>
<div class="newsCat" id="newsCat1">Politics</div>
<div class="newsCat" id="newsCat2">Sports</div>
<div class="newsCat" id="newsCat3">Lifestyle</div>
<div id="newsLoading">Loading
<img src="loading_indicator.gif" title="Loading..." alt="Loading..." border="0" />
</div>
<div id="newsContent"></div>
</div>
</body>
</html>
这页是包括在index.php所需页面!
内容load.php
<?php
function stringForJavascript($in_string) {
$str = preg_replace("# [\r\n] #", " \\n\\\n", $in_string);
$str = preg_replace('#"#', '\\"', $str);
return $str;
$user = $_SESSION['userName'];
}
switch($_GET['id']) {
case 'newsCat1':
$content = include("politics.php");
break;
case 'newsCat2':
$content = include("sports.php");
break;
case 'newsCat3':
$content = include("lifestyle.php");
break;
default:
$content = 'There was an error.';
}
print stringForJavascript($content);
usleep(600000);
?>
问题面临的
它工作正常,但是当我刷新页面或提交表单,代码刷新,我指的是网页其中包括前清爽,不存在......
我真的很抱歉我的英语,我知道它太可怕了! :)
请帮助我的PHP的主人,我需要UR帮助...
一个cookie,嗯thanx的建议... :) –
yipppii .... thanx fr你的建议@Eugen Rieck,它的工作! :) –