简体   繁体   English

通过 AJAX 加载的 HTML 内容无序加载外部 JavaScript

[英]HTML content loaded via AJAX loads external JavaScript out of order

Here's the scenario, not sure what I'm missing.这是场景,不确定我错过了什么。

  • Page A.htm makes an ajax request for page B.htm, and inserts the response into the page.页面 A.htm 向页面 B.htm 发出 ajax 请求,并将响应插入到页面中。

  • Page B.htm contains links to several other JS files, many of which contain a document.ready() function to initialize them.页面 B.htm 包含指向其他几个 JS 文件的链接,其中许多包含用于初始化它们的 document.ready() 函数。

This works fine when A.htm and B.htm are on the same server but not when they are on different servers.这在 A.htm 和 B.htm 在同一台服务器上时工作正常,但在不同服务器上时则不然。

What I think I'm seeing here, is that when page A and B are on different servers (cross domain ajax), the external resources are being returned asynchronously, or at least out of order, so scripts are executing expecting JQuery.UI to be loaded already, when it is not.我在这里看到的是,当页面 A 和 B 在不同的服务器(跨域 ajax)上时,外部资源是异步返回的,或者至少是乱序返回的,因此脚本正在执行,期望 JQuery.UI已经加载,当它不是。

Appreciate any pointers or advice.感谢任何指示或建议。 Apologies for the poor explanation.为糟糕的解释道歉。

You are injecting HTML + script tags via jQuery.您正在通过 jQuery 注入 HTML + 脚本标签。 In this case *:在这种情况下 *:

  • HTML content except scripts are injected in the document除了脚本之外的 HTML 内容都被注入到文档中
  • Then all scripts are executed one by one然后所有脚本一一执行
  • If a script is external then it is downloaded and executed asynchronously如果脚本是外部的,那么它会被异步下载和执行

Therefore an external or inline script that depends on jQuery UI might execute before jQuery UI.因此,依赖于 jQuery UI 的外部或内联脚本可能会在 jQuery UI 之前执行。

One possible solution is to change the way your pages work:一种可能的解决方案是更改页面的工作方式:

  • Get rid of external scripts in pageb.html but keep inline scripts摆脱 pageb.html 中的外部脚本,但保留内联脚本
  • Load the required scripts in pagea.html在 pagea.html 中加载所需的脚本
  • Load pageb.html加载 pageb.html

Another solution is to roll your own jQuery function that will:另一种解决方案是推出您自己的 jQuery 函数,它将:

  • Strip all <script src> elements from HTML从 HTML 中去除所有<script src>元素
  • Download and execute those scripts in order按顺序下载并执行这些脚本
  • Inject the remaining HTML注入剩余的 HTML

* The exact behavior is not documented. * 没有记录确切的行为。 I had to look into the source code to infer the details.我不得不查看源代码来推断细节。

you are correct in your impression that the issue is a difference in how the requests are handled cross-domain.您认为问题在于跨域处理请求的方式不同,这是正确的。

Here is a link to get you on the right track : How to make synchronous JSONP crossdomain call这是一个让您走上正轨的链接: 如何进行同步 JSONP 跨域调用

However, you will have to actually re-achitect your solution somewhat to check if the resource has been loaded before moving on.但是,您实际上必须重新构建您的解决方案,以在继续之前检查资源是否已加载。 There are many solutions (see the link)有很多解决方案(见链接)

You can set a timer interval and check for something in the dom, or another reasonable solution (despite it's lack of efficiency) is to create a "proxy" serverside (eg php) file on your server and have that file do the cross-domain request, then spit out the result.您可以设置一个计时器间隔并检查 dom 中的某些内容,或者另一个合理的解决方案(尽管它缺乏效率)是在您的服务器上创建一个“代理”服务器端(例如 php)文件并让该文件进行跨域请求,然后吐出结果。

Note that since jquery UI is a rather large file, it's conceivable that the cross-domain request finishes first, and executes immediately, even though jqueryUI is not loaded yet.请注意,由于 jquery UI 是一个相当大的文件,因此可以想象跨域请求首先完成,并立即执行,即使尚未加载 jqueryUI。 In any case, you're going to have to start thinking about having your app react rather than follow a sequence.在任何情况下,您都将不得不开始考虑让您的应用做出反应,而不是遵循一个顺序。

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

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