简体   繁体   English

动态地同步加载来自不同域的JavaScript文件

[英]Dynamically and synchronously load JavaScript file from a different domain

I would like to synchronously include a JavaScript file from a different domain via code. 我想通过代码同步包含来自不同域的JavaScript文件。 This means that using a synchronous XMLHttpRequest will not work. 这意味着使用同步XMLHttpRequest将无法正常工作。 I also want to avoid document.write because my code will be executed when the document is fully loaded. 我还想避免使用document.write因为我的代码将在文档完全加载时执行。 Is this even possible? 这甚至可能吗? Does any of the existing JavaScript libraries support that feature? 是否有任何现有的JavaScript库支持该功能?

Basically I want this to work: 基本上我希望这个工作:

<script type="text/javascript">
  $(document).ready(function() {
      load("path_to_jQuery_UI_from_another_domain");
      console.log(jQuery.ui.version); //outputs the version of jQuery UI
  });
</script>

EDIT: My idea is to create a jQuery plugin which loads its JavaScript files based on the enabled features. 编辑:我的想法是创建一个jQuery插件,根据启用的功能加载其JavaScript文件。 jQuery plugins can be initialized at any time which means no document.write. jQuery插件可以随时初始化,这意味着没有document.write。 It is perfectly fine to load the JavaScript files asynchronously but people expect their plugins to be fully initialized after calling $("selector").something(); 完全可以异步加载JavaScript文件,但人们希望在调用$("selector").something();之后完全初始化它们的插件$("selector").something(); . Hence the need of synchronous JavaScript loading without document.write. 因此需要在没有document.write的情况下加载同步JavaScript。 I guess I just want too much. 我想我只是想要太多。

The only way to synchonously load files is to document.write a script tag into your page. 同步加载文件的唯一方法是document.write脚本标记到您的页面。 This is generally considered a bad practice. 这通常被认为是一种不好的做法。 There is probably a better way to do what you actually want, but, in the spirit of transparency: 可能有更好的方法来做你真正想要的事情,但是,本着透明的精神:

document.write('<script src="http://otherdomain.com/script.js"></'+'script>')

should do the trick. 应该做的伎俩。 You have to escape the closing script tag so the parser doesn't close the script tag that you wrote. 您必须转义结束脚本标记,以便解析器不会关闭您编写的脚本标记。

**Note that you can't dynamically load scripts that contain a document.write **请注意,您无法动态加载包含document.write脚本

You should be able to use .getScript() 你应该可以使用.getScript()

Edit: Cross-domain requests are always loaded asynchronously in jQuery. 编辑:跨域请求总是在jQuery中异步加载。

A great library called YepNope exists for loading javascript dependencies from any location - developed by a member of the yayQuery podcast. 一个名为YepNope的大型库可用于从任何位置加载javascript依赖项 - 由yayQuery播客的成员开发。 It can be found here: http://yepnopejs.com/ 它可以在这里找到:http: //yepnopejs.com/

LABjs.js, is nice library. LABjs.js,是一个不错的图书馆。 I used it works well. 我用它效果很好。

http://labjs.com/ http://labjs.com/

It's not possible to synchronously execute a script at a URL. 无法在URL上同步执行脚本。 Note further that synchronous anything, when networks (or even file systems!) are involved is a Bad Idea. 进一步注意,当涉及网络(甚至文件系统!)时,同步任何东西都是一个坏主意。 Someone, sometime, somewhere will be on a slow system, or a slow network, or both, and suddenly you've just hung their UI in the process. 某个人,某个时候,某个地方将处于一个缓慢的系统,或一个缓慢的网络,或两者兼而有之,突然间你只是在这个过程中挂起他们的UI。

Synchronous is bad. 同步很糟糕。 Asynchronous with callbacks is good. 异步回调很好。

Note that, as a worst-case hack, you could overwrite $ with your own function, which returned an object with just the right properties, and you could semi-lazily evaluate all actual calls. 请注意,作为最坏情况的黑客,您可以使用自己的函数覆盖$ ,该函数返回具有恰当属性的对象,并且您可以半懒惰地评估所有实际调用。 This of course breaks if you start relying on immediate execution of the calls, or on their execution being intermingled with the evaluation of arguments, but in the worst case it's not completely implausible. 如果你开始依赖于立即执行调用,或者他们的执行与参数的评估混合在一起,这当然会中断,但在最坏的情况下,它并非完全不可信。

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

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