问题
I have a web application that has several iframes that all need to import the same javascript library (eg jquery).
Is there a way to only load it once and somehow share that data across all the iframes? I'd rather not have my page load slowly because it is loading the same JS files once for every iframe.
Thanks!
EDIT: People are saying you can't share data from an iframe, but what if the js imports are all in a namespace say NAMESPACE and then the iframe does something like NAMESPACE = parent.NAMESPACE
回答1:
If all frames are loading the same js file, modern browsers (chrome, firefox, IE) should simply load the same file from the cache...so you don't have to re-load the same file over and over again. If your page load times are long, consider minifying your javascript (making your js files smaller through the use of another program)...this can greatly reduce load times.
回答2:
Assuming that you src the same URI, and you have sane cache control headers, then the JS will be cached and not re-downloaded for each frame.
That said, if you really wanted to, you could load it in the top frame, and then access everything via the parent object.
For example: http://dorward.me.uk/tmp/frames/top.html
来源:https://stackoverflow.com/questions/11348512/possible-to-share-javascript-imports-across-iframes