简体   繁体   English

如何动态加载来自不同域的javascript文件?

[英]How do you dynamically load a javascript file from different domain?

I find this excellent code, posted by aemkei as answers to this questions: 我发现这个优秀的代码,由aemkei发布,作为这些问题的答案:

  1. How do you dynamically load a javascript file? 你如何动态加载JavaScript文件? (Think C's #include) (想想C的#include)
  2. Use javascript to inject script references as needed? 使用javascript根据需要注入脚本引用?

You may write dynamic script tags (using Prototype): 您可以编写动态脚本标记(使用Prototype):

 new Element("script", {src: "myBigCodeLibrary.js", type: "text/javascript"}); 

The problem here is that we do not know when the external script file is fully loaded. 这里的问题是我们不知道外部脚本文件何时完全加载。

We often want our dependant code on the very next line and like to write something like: 我们经常希望我们的依赖代码在下一行,并喜欢写下这样的东西:

 if (iNeedSomeMore){ Script.load("myBigCodeLibrary.js"); // includes code for myFancyMethod(); myFancyMethod(); // cool, no need for callbacks! } 

There is a smart way to inject script dependencies without the need of callbacks. 有一种智能方法可以在不需要回调的情况下注入脚本依赖项。 You simply have to pull the script via a synchronous AJAX request and eval the script on global level. 您只需通过同步AJAX请求拉出脚本并在全局级别上评估脚本。

If you use Prototype the Script.load method looks like this: 如果使用Prototype,则Script.load方法如下所示:

 var Script = { _loadedScripts: [], include: function(script){ // include script only once if (this._loadedScripts.include(script)){ return false; } // request file synchronous var code = new Ajax.Request(script, { asynchronous: false, method: "GET", evalJS: false, evalJSON: false }).transport.responseText; // eval code on global level if (Prototype.Browser.IE) { window.execScript(code); } else if (Prototype.Browser.WebKit){ $$("head").first().insert(Object.extend( new Element("script", {type: "text/javascript"}), {text: code} )); } else { window.eval(code); } // remember included script this._loadedScripts.push(script); } }; 

I found that, the code does not work on IE if the all of them is executed in 'file://' protocol, however, it is not the problem since its use case involved real web application. 我发现,如果所有这些代码都在'file://'协议中执行,那么代码在IE上不起作用,但是,它不是问题,因为它的用例涉及真正的Web应用程序。

I tried it once to include http://www.google-analytics.com/urchin.js by google, but from one of web page, but it looks like it cannot request javascript file from different domain. 我尝试了一次,包括谷歌的http://www.google-analytics.com/urchin.js ,但是从一个网页,但看起来它不能从不同的域请求javascript文件。

How we could dynamically add javascript, just like what above scripts does, but from another domain? 我们如何动态添加javascript,就像上面的脚本一样,但是来自另一个域?

You can use the onload and onreadystatechange event to understand when the <script> tag is loaded. 您可以使用onloadonreadystatechange事件来了解何时加载<script>标记。

var script = new Element("script", {src: "myBigCodeLibrary.js", type: "text/javascript"});

script.onload = script.onreadystatechange = function(){
    if (!this.readyState ||
        this.readyState == "loaded" || this.readyState == "complete") {
        //script is loaded
    }
};

The security model in modern browsers prevents JavaScript from making cross-domain requests. 现代浏览器中的安全模型可防止JavaScript发出跨域请求。 That has holes (see every website exploit since the beginning of the internet), but using them is more than a little shady and it's only a matter of time before they're patched. 这有漏洞(从互联网开始以来看到每个网站的漏洞利用),但使用它们不仅仅是一点点阴暗,而且它们在修补之前只是时间问题。

What Rex said is correct, although HTML5 has added cross domain messaging and xhr, which require a little bit of work on your part but should be usable to achieve this. Rex所说的是正确的,尽管HTML5已经添加了跨域消息传递和xhr,这需要您做一些工作,但应该可以实现这一点。 Alas they're not yet present in all released browsers (i think the latest betas of safari, firefox, and IE have support for some of these features, but i'm not sure which browsers support which apis) 唉他们还没有出现在所有发布的浏览器中(我认为safari,firefox和IE的最新测试版支持其中一些功能,但我不确定哪些浏览器支持哪种apis)

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

相关问题 动态地同步加载来自不同域的JavaScript文件 - Dynamically and synchronously load JavaScript file from a different domain 如何将数据从文本文件加载到javascript中的变量中 - How do you load data from a text file into a variable in javascript 您如何将javascript文件加载到范围中 - How do you load a javascript file into a scope 如何使用.load()从其他域获取内容? - How do you get content from another domain with .load()? 如何使用 java 脚本从不同的 html 文件动态加载引导模式内容? - How do I load bootstrap modal content dynamically from a different html file using java script? 如何在$ .ajax()的成功函数中加载Javascript文件? - How do you load a Javascript file inside of the success function for $.ajax()? 如何安全地加载外部 JavaScript 文件? - How do you safely load external JavaScript file? 从bookmarklet中的其他域加载javascript文件? - Load javascript file from other domain in bookmarklet? 如何动态加载具有属性的js文件并在加载时执行回调? - How do you dynamically load a js file with attributes and execute a callback on load? 如何从上下文路径获取 javascript 文件名并动态加载 - how to get javascript file names from context path and load it dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM