Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Ajax XMLHttpRequest中包含js文件_Php_Ajax_Xmlhttprequest - Fatal编程技术网

Php 在Ajax XMLHttpRequest中包含js文件

Php 在Ajax XMLHttpRequest中包含js文件,php,ajax,xmlhttprequest,Php,Ajax,Xmlhttprequest,我正在使用Ajax和下面描述的代码。我想在Ajax XMLHttpRequest中包含.js文件。有人知道怎么做吗 例如: 我的代码如下: function getXMLHttp() { var xmlHttp try { //Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); } catch(e) { //Internet Explorer try {

我正在使用Ajax和下面描述的代码。我想在Ajax XMLHttpRequest中包含.js文件。有人知道怎么做吗

例如: 我的代码如下:

function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest(id)
{
  var xmlHttp = getXMLHttp();

  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", "updatesite.admin.php?id="+id, true);
  xmlHttp.send(null);
}

function HandleResponse(response)
{
    document.getElementById('Vsebina').innerHTML = response;
}
当程序调用函数MakeRequest(id)时,我还想执行一些.js文件。
可以这样做吗?

您可以将此代码放在函数中,以使脚本加载并执行

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://location-of-script.js";
document.body.appendChild(script);
这里有一个完整的例子。这两个文件只需要位于服务器上的同一文件夹中

这是hello.html:

<html>
<head>
<script type="text/javascript">
function doit()
{
  var scr = document.createElement('script');
  scr.type = "text/javascript";
  scr.src = "hello.js";
  document.body.appendChild(scr);
}
</script>
</head>
<body>
<input type="button" value="hello" onclick="doit();">
</body>
</html>

您可以将此代码放在函数中,以便加载和执行脚本

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://location-of-script.js";
document.body.appendChild(script);
这里有一个完整的例子。这两个文件只需要位于服务器上的同一文件夹中

这是hello.html:

<html>
<head>
<script type="text/javascript">
function doit()
{
  var scr = document.createElement('script');
  scr.type = "text/javascript";
  scr.src = "hello.js";
  document.body.appendChild(scr);
}
</script>
</head>
<body>
<input type="button" value="hello" onclick="doit();">
</body>
</html>

“我想在Ajax XMLHttpRequest中包含.js文件。”您的意思是“我想使用
innerHTML
将js添加到页面并让它执行”吗?XMLHttpRequest似乎有点像是在转移注意力。所谓“我想在Ajax XMLHttpRequest中包含.js文件”,是指“我想使用
innerHTML
将js添加到页面并让它执行”吗?XMLHttpRequest似乎有点像是在转移注意力。谢谢你的回答,但这没有帮助。是的,在一个更完整的示例中测试它,只是为了确保我没有在地板上掉任何东西。谢谢你的回答,但这没有帮助。是的,在一个更完整的示例中测试它,只是为了确保我没有在地板上掉任何东西。