Search code examples
phpjavascriptsrcdocument.write

javascript calling a php file in src , the proper way


I am including the following javascript code in my HTML , which in turn makes a reference to a php file for doing a backend job :

<script type='text/javascript'>
<!--//<![CDATA[
var partnerId = "100b70a8a2248717";
var siteId = "12418";
var m3_u =    (location.protocol=='https:'?'https://javascriptGetAd.php':'http://javascriptGetAd.php');
var m3_r = Math.floor(Math.random()*99999999999);
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?partner_id=" + partnerId);
document.write ('&amp;site_id=' + siteId);
document.write ('&amp;version=1.5');
document.write ('&amp;language=javascript');
document.write ('&amp;format=wap');
document.write ('&amp;cb=' + m3_r);
document.write ("'><\/scr"+"ipt>");
//]]>-->

Can I call javascriptGetAd.php from inside a javascript function in any other way without using document.write() as shown above ?


Solution

  • You can use AJAX for this purpose

        var xmlhttp;
        if (window.XMLHttpRequest)
            xmlhttp=new XMLHttpRequest();
        else
           xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    
    
    
    
      xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
             // Handle xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET","javascriptGetAd.php",true);
        xmlhttp.send();