阿贾克斯引发身体的onLoad事件 [英] Ajax triggering body's onLoad event
问题描述
我的整个页面再次加载每当我做一个AJAX调用加载一个div。 我注意到,身体的onload =的init()onload事件是越来越触发Ajax响应,所有的初始化再次发生。我不希望这样的事情发生。 有没有通过,只有DIV通过Ajax调用加载的方式。
<身体的onload =的init()>
.....
.....
< DIV> ...< A HREF =的onclick =saveView('更多')>< B>详情< / B>< / A>< / DIV>
< /身体GT;
main.js
函数saveView(ARG){
如果(ARG ==多){
ajaxGet(baseRef +all.html,loadList);
}其他{
ajaxGet(baseRef +全A.html,loadList);
}
功能的init(){
.....
}
功能ajaxGet(URL,ResponseHandler所)
{
VAR page_request = FALSE;
如果(window.XMLHtt prequest和放大器;及(window.ActiveXObject&安培;&安培; window.location.protocol ==文件:!)){
只有当可用//使用这个,而不是使用IE的本地文件系统
page_request =新XMLHtt prequest();
}
否则,如果(window.ActiveXObject){// IE或IE对本地文件系统旧版本
尝试 {
page_request =新的ActiveXObject(MSXML2.XMLHTTP);
}
赶上(E){
尝试{
page_request =新的ActiveXObject(Microsoft.XMLHTTP);
}
赶上(E){
}
}
}
其他 {
警报(您的浏览器不支持XMLHTTP。);
返回false;
}
page_request.onreadystatechange =功能(){
如果(page_request.readyState == 4){
//在本地计算机上成功的状态是0 Web服务器上的200
如果(page_request.status == 200 || page_request.status == 0){
ResponseHandler所(page_request);
}
}
}
page_request.open(GET,URL,真正的);
page_request.send(空);
}
功能loadList(page_request){
的document.getElementById(名单)的innerHTML = page_request.responseText。
加载=真正的;
尝试{
如果(pLoaded)
doFilterStateChange1();
}赶上(五)
{
}
的setTimeout(restoreScrollTop(),1000);
}
这不是阿贾克斯所引发身体的onLoad事件。 如果你看到的锚标记,我没有分配任何价值的HREF =,这是导致页面再次加载。删除它解决了这个问题。
My whole page is loading again whenever I make a ajax call to load a div. I have noticed that 'body onload=init()' onload event is getting triggered on ajax response and all the initialization is happening again. I don't want that to happen. Is there a way by which only div is loaded through ajax call.
<body onload="init()">
.....
.....
<div>...<a href="" onclick="saveView('more')"><b>More</b></a></div>
</body>
main.js
function saveView(arg){
if(arg=="more"){
ajaxGet(baseRef+"all.html", loadList);
}else{
ajaxGet(baseRef+"all-A.html", loadList);
}
function init(){
.....
}
function ajaxGet(url, responseHandler)
{
var page_request = false;
if (window.XMLHttpRequest && !(window.ActiveXObject && window.location.protocol == "file:")) {
// use this only if available, and not using IE on a local filesystem
page_request = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // older versions of IE, or IE on a local filesystem
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
}
}
}
else {
alert("Your browser does not support XMLHTTP.");
return false;
}
page_request.onreadystatechange=function() {
if(page_request.readyState==4) {
// on local machines the status for success is 0. on web servers it is 200
if(page_request.status==200 || page_request.status==0) {
responseHandler(page_request);
}
}
}
page_request.open('GET', url, true);
page_request.send(null);
}
function loadList(page_request){
document.getElementById("list").innerHTML=page_request.responseText;
Loaded = true;
try{
if(pLoaded)
doFilterStateChange1();
}catch(e)
{
}
setTimeout("restoreScrollTop()", 1000);
}
It is not ajax that is triggering onLoad event of body. If you see the anchor tag, I haven't assigned any value to href="" which was causing the page to be loaded again. Removing it solved the problem.
这篇关于阿贾克斯引发身体的onLoad事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!