简体   繁体   English

如何将通过ajax或jquery.load加载的所有javascript放在非全局子范围内

[英]How to put all of the javascript loaded through ajax or jquery.load in a non-global child scope

I'm trying to get around iframe by using ajax/jquery.load() and inject a third party page on my site (CORS requirements are not a issues here as the third party sets the appropriate CORS response headers). 我正在尝试使用ajax / jquery.load()解决iframe问题,并在我的网站上注入第三方页面(CORS要求在这里不是问题,因为第三方设置了适当的CORS响应标头)。

So my question is: Let's say that the main host site loads jquery 1.x and a bunch of other libs. 所以我的问题是:假设主宿主站点加载了jquery 1.x和许多其他库。 And let's say that the third party page loads jquery 2.x and some other conflicting libraries with the main site. 假设第三方页面在主站点上加载了jquery 2.x和其他一些冲突的库。

How can I refactor my JS on the third party page so that it's not loadedin JS global scope but in a shielded child scope. 我如何在第三方页面上重构我的JS,以便它不被加载到JS全局范围中,而是被屏蔽的子范围中。

Can I wrap everything in function(){}?? 我可以将所有内容包装在function(){}中吗? Including the jquery libs etc? 包括jQuery库等? If so how do I access the methods/vars in this child scope on the third party page? 如果是这样,如何在第三方页面的此子范围内访问方法/变量?

Let me know if I need to provide any more details... 让我知道是否需要提供更多详细信息...

iframes by default load an entire document on their own, and are pretty much shielded to everything, you can allow modals/popup/etc with the sandbox attribute, if none specified it will just show content, and won't interact with the upper layer. 默认情况下, iframes自行加载整个document ,并且几乎不受所有内容的影响,您可以允许使用sandbox属性使用modals / popup / etc,如果未指定,则只会显示内容,并且不会与上层进行交互。

To access the function inside an iframe , you can: 要访问iframe的函数,您可以:

document.getElementById("resultFrame").contentWindow.Reset()

This will run the Reset() Global Function inside the iframe with resultFrame ID. 这将在带有resultFrame ID的iframe中运行Reset()全局函数。

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

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