简体   繁体   English

javascript在src中调用php文件,正确的方法

[英]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 : 我在HTML中包含以下javascript代码,这反过来又引用了用于执行后端工作的php文件:

<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 ? 我可以以任何其他方式从javascript函数内部调用javascriptGetAd.php而不使用如上所述的document.write()吗?

You can use AJAX for this purpose 您可以为此目的使用AJAX

    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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM