Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX and callback function
    text
    copied!<p>I will try to explain my problem. I have 4 files index.html, event.js, start.js and form.php.</p> <p>index.html</p> <pre><code>&lt;head&gt;&lt;script type="javascript/text" src="start.js&gt;&lt;/script&gt;&lt;/head&gt; &lt;html&gt; &lt;button id="bt" type="button"&gt;Click&lt;/button&gt; &lt;div id="test"&gt;&lt;/div&gt; &lt;/html&gt; </code></pre> <p>start.js</p> <pre><code>window.onload=init; function init(){ document.getElementById("bt").onclick=function(){ getFile('form.php?x='+Math.random()*11, getPHP); getFile('javascripts/event.js?x='+Math.random()*11,getJS); } } function getFile(source,callFunc){ var xmlHttpR=false; if (window.XMLHttpRequest) xmlHttpR=new XMLHttpRequest(); else if (window.ActiveXObject) xmlHttpR=new ActiveXObject("Microsoft.XMLHttp"); else alert("Error"); if (xmlHttpR){ xmlHttpR.open("GET",source,true); xmlHttpR.onreadystatechange=function(){ if (xmlHttpR.readyState==4 &amp;&amp; xmlHttpR.status==200){ callFunc(xmlHttpR); delete xmlHttpR; xmlHttpR=null; } } } xmlHttpR.send(null); } function getPHP(response){ document.getElementById("test").innerHTML=response.responseText; } function getJS(response){ eval(response.responseText); } </code></pre> <p>event.js</p> <pre><code>document.getElementById("firstName").onblur=function(){ validate(this.name,this.value); } </code></pre> <p>form.php</p> <pre><code>&lt;html&gt; &lt;form&gt; &lt;input type="text" id="firstName" value=""&gt; &lt;/form&gt; &lt;/html&gt; </code></pre> <p>OK, the problem now, when onblur event is triggered on firstName nothing happens but when I refresh the page sometimes once or twice, the event is working as it should be. Maybe I'm getting the response not at the right time?</p> <p>Is there something wrong with the callback function and the eval and I use to assign onblur event to the firstName field?</p> <p>is the problem with the AJAX asynchronous call? Should I use synchronous? What am I missing?</p> <p>I hope I could explain clearly what the problem is.</p> <p>Thanks in advance for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload