XMLHttpRequest生成具有更多XMLHttpRequest请求的HTML [英] XMLHttpRequest generates HTML with more XMLHttpRequest requests

查看:77
本文介绍了XMLHttpRequest生成具有更多XMLHttpRequest请求的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下代码:

<div id="dashboard">
  Loading Dashboard.....
</div>
<script>
  var xhttpDashboard = new XMLHttpRequest();
  xhttpDashboard.onreadystatechange = function() {
    if (xhttpDashboard.readyState == 4 && xhttpDashboard.status == 200) {
      document.getElementById("dashboard").innerHTML = xhttpDashboard.responseText;
    }
  };
  xhttpDashboard.open("GET", "https://www.example.com/getdashboard.html", true);
  xhttpDashboard.send();
</script>

getdashboard.html 返回html,而html包含更多要执行的JavaScript XMLHttpRequest.我发现这第二组XMLHttpRequests没有执行.

getdashboard.html returns html which in turn contains more javascript XMLHttpRequest to be executed. I am finding these 2nd sets of XMLHttpRequests are not executing.

如何获取第二套Java脚本来执行?

How can I get the 2nd set of javascript to execute?

更新发现这篇文章很有用: https://24ways.org/2005/have-your-dom和脚本太

Update Found this article to be useful: https://24ways.org/2005/have-your-dom-and-script-it-too

推荐答案

如果使用jQuery,则有一个简单的答案.

There is a simple answer if you use jQuery.

使用不带后缀选择器表达式的URL调用.load()时,内容将在删除脚本之前传递给.html().

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed.

因此,使用jQuery,您可以轻松做到:

So with jQuery you could simply do:

<div id="dashboard">
  Loading Dashboard.....
</div>
<script>
$("#dashboard").load("https://www.example.com/getdashboard.html");
</script>

,脚本标签将被执行.

此功能的文档位于: https://api.jquery.com/load/

这篇关于XMLHttpRequest生成具有更多XMLHttpRequest请求的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆