ajax - Javascript onload two functions not returning the data -


i trying display 2 javascript function when page load. seems 1 overwriting one.

here javascript code:

function welcome(){      if (window.xmlhttprequest)        {// code ie7+, firefox, chrome, opera, safari        xmlhttp=new xmlhttprequest();        }     else        {// code ie6, ie5        xmlhttp=new activexobject("microsoft.xmlhttp");        }      xmlhttp.onreadystatechange=function()        {     if (xmlhttp.readystate==4 && xmlhttp.status==200)         {          document.getelementbyid("welcome").innerhtml=xmlhttp.responsetext;         }      }      xmlhttp.open("get","php/display_welcome.php", true);      xmlhttp.send(); }   function testimonial(){     if (window.xmlhttprequest)        {// code ie7+, firefox, chrome, opera, safari        xmlhttp=new xmlhttprequest();        }     else        {// code ie6, ie5        xmlhttp=new activexobject("microsoft.xmlhttp");        }      xmlhttp.onreadystatechange=function()        {     if (xmlhttp.readystate==4 && xmlhttp.status==200)         {          document.getelementbyid("testimonial").innerhtml=xmlhttp.responsetext;         }      }      xmlhttp.open("get","php/display_testimonial.php", true);      xmlhttp.send();  }  window.onload = function() {     welcome();     testimonial(); }; 

i have 2 div id in html page. using ie9, when run 1 function works fine, when run 2 doesnt return data. hope me.

a lot have gone wrong:

  • non-responsive server
  • bad copy-pasted code
  • syntax errors
  • etc.

anyways, avoid repepetive code , possible errors if both code looks same, suggest this. since both use same operation, let's factor out single reusable function:

function load(url,callback) {    var xmlhttp;   if (window.xmlhttprequest) xmlhttp = new xmlhttprequest();   else xmlhttp = new activexobject("microsoft.xmlhttp");    xmlhttp.onreadystatechange = function () {     if (xmlhttp.readystate === 4 && xmlhttp.status === 200) {       callback(xmlhttp.responsetext);     }   }   xmlhttp.open('get', url);   xmlhttp.send(); }  window.onload = function () {    load('php/display_welcome.php',function(response){     document.getelementbyid("welcome").innerhtml = response;   });    load('php/display_testimonial.php',function(response){     document.getelementbyid("testimonial").innerhtml = response;   });  }; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -