2012-08-15 91 views
1

对不起,真正新手的问题,刚开始学习AJAX。为什么body onload = javascript函数连续执行?

我想了解下列原因导致divMessage内容在“myName”文本更改时不断更改。 1)Javascript函数过程似乎在不断“听”,这是正常的Javscript行为,还是有触发器重复调用“过程”? 2)我们分配给“body onload”的任何函数是否会重复执行?这种重复执行的频率如何? 3)如果我们想单个执行函数过程,该怎么做?

4)我很困惑,因为我认为身体只会加载一次。但这是因为在“body onload”之后调用了函数“process”,函数进程又通过改变divMessage来修改“body”,实质上让它再次通过“body onload”,然后再“处理”等等。?

非常感谢您的帮助!

<head> 
    <script type="text/javascript" src="quickstart.js"></script> 
</head> 
<body onload='process()'> 
    Server wants to know your name: 
    <input type="text" id="myName" /> 
    <div id="divMessage" /> 
</body> 

这里的quickstart.js加工零件

function process() 
{ 
    // proceed only if the xmlHttp object isn't busy 
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
    { 
    // retrieve the name typed by the user on the form 
    name = encodeURIComponent(
    document.getElementById("myName").value); 
    // execute the quickstart.php page from the server 
    xmlHttp.open("GET", "quickstart.php?name=" + name, true); 
    // define the method to handle server responses 
    xmlHttp.onreadystatechange = handleServerResponse; 
    // make the server request 
    xmlHttp.send(null); 
    } 
} 

    // callback function executed when a message is received from the server 
    function handleServerResponse() 
    { 
    // move forward only if the transaction has completed 
    if (xmlHttp.readyState == 4) 
    { 
     // status of 200 indicates the transaction completed successfully 
     if (xmlHttp.status == 200) 
     { 
     xmlResponse = xmlHttp.responseXML; 
     xmlDocumentElement = xmlResponse.documentElement; 
     helloMessage = xmlDocumentElement.firstChild.data; 

     // display the data received from the server 
     document.getElementById("divMessage").innerHTML = 
        '<i>' + helloMessage+ '</i>'; 
     } 
    } 
    } 

和quickstart.php

<?php 
header('Content-Type: text/xml'); 
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'; 
echo '<response>'; 
$name = $_GET['name']; 

// generate output depending on the user name received from client 
$userNames = array('YODA', 'AUDRA', 'BOGDAN', 'CRISTIAN'); 
if (in_array(strtoupper($name), $userNames)) 
    echo 'Hello, master ' . htmlentities($name) . '!'; 
else if (trim($name) == '') 
    echo 'Stranger, please tell me your name!'; 
else 
    echo htmlentities($name) . ', I don't know you!'; 

echo '</response>'; 
?> 
+0

它(文档加载事件)应该只执行一次,除了一些浏览器的怪癖(旧的IE和iframe?)。我知道如果重新打开文档对象(如document.write)应该重新触发它,但DOM操作(即使'innetHTML')通常不应该..或者我可能已经使用jQuery这么长时间了,我忘记了如何破碎很简单,IDK。 – 2012-08-15 17:03:10

+0

在任何情况下,请使用浏览器标记这是观察。 – 2012-08-15 17:04:05

+0

请参阅:http://stackoverflow.com/questions/5418216/ajax-triggering-bodys-onload-event(也许)。或者,从其他任何位置调用进程?(除onload之外)? – 2012-08-15 17:15:16

回答

0

通过关闭并试图再次运行此找到了答案。 原来有,我注释掉 额外的代码,但被缓存调用“过程”从handleServerResponse(每隔1秒):

// display the data received from the server 
    document.getElementById("divMessage").innerHTML = 
       '<i>' + helloMessage+ '</i>'; 
    setTimeout('process()', 1000); 

对此深感抱歉。教训是在测试每个代码更改之前清除浏览器高速缓存:) 所以我猜测这个函数是否被其他地方调用的建议是正确的。 无论如何,我读了很多东西,并试图将 列在这里。非常感谢@pst!

相关问题