I'm writing a web app, using an embedded web server that does not permit me to edit http header and so use file caching. My app uses an iframe, but the problem is that every time I change the iframe's src, it reload every libraries I'm using, even if they are loaded in the parent window.
我正在编写一个Web应用程序,使用的嵌入式Web服务器不允许我编辑http标头,因此使用文件缓存。我的应用程序使用iframe,但问题是每次我更改iframe的src时,它会重新加载我正在使用的每个库,即使它们是在父窗口中加载的。
I know I can get JQuery back with :
我知道我可以通过以下方式获得JQuery:
var jQuery = window.parent.jQuery;
var jQuery = window.parent.jQuery;
Any suggestions to do the same with every JavaScript files ?
有没有建议对每个JavaScript文件做同样的事情?
1 个解决方案
#1
0
Since you can access the window scope you can access every library that has it's reference stored there.
由于您可以访问窗口范围,因此您可以访问存储在其中的引用的每个库。
Example for underscore.js.
underscore.js的示例。
var underscore = window.parent._;
You even can create your own references like this:
您甚至可以像这样创建自己的引用:
(function(myLibrary){
//anonymous scope
var doStuff = function(whatStuff){
whatStuff = whatStuff || 'defaultStuff';
alert('Doing ' + whatStuff);
};
//adding stuff to my library
myLibrary.doStuff = doStuff;
//publishing myLibrary to the desired scope
//iframe's parent
window.parent.myLibrary = myLibrary;
//current window
window.myLibrary = myLibrary;
}({}))
But why? Here's some more info how stuff works:
但为什么?这里有一些更多的信息如何工作:
Possible to share javascript imports across iframes?
可以在iframe*享javascript导入吗?
How to use jquery from parent inside an iframe?
如何在iframe中使用父级的jquery?
https://www.sitepoint.com/jquery-sharing-parents-iframes-inherit-js/
#1
0
Since you can access the window scope you can access every library that has it's reference stored there.
由于您可以访问窗口范围,因此您可以访问存储在其中的引用的每个库。
Example for underscore.js.
underscore.js的示例。
var underscore = window.parent._;
You even can create your own references like this:
您甚至可以像这样创建自己的引用:
(function(myLibrary){
//anonymous scope
var doStuff = function(whatStuff){
whatStuff = whatStuff || 'defaultStuff';
alert('Doing ' + whatStuff);
};
//adding stuff to my library
myLibrary.doStuff = doStuff;
//publishing myLibrary to the desired scope
//iframe's parent
window.parent.myLibrary = myLibrary;
//current window
window.myLibrary = myLibrary;
}({}))
But why? Here's some more info how stuff works:
但为什么?这里有一些更多的信息如何工作:
Possible to share javascript imports across iframes?
可以在iframe*享javascript导入吗?
How to use jquery from parent inside an iframe?
如何在iframe中使用父级的jquery?
https://www.sitepoint.com/jquery-sharing-parents-iframes-inherit-js/