gespinha
gespinha

Reputation: 8487

Is it possible to load Jquery from iFrame?

I have an HTML document loading the JQuery library and in this document I have an iframe pointed at another HTML document that also needs the JQuery Library and several other .js files that are also in the main HTML document.

This process obviously makes the browser download the same file two times. And most Site Speed Tests indicate this is may be an issue.

How can I make the browser only have one file? If I leave just one script link in the main HTML will it work?

Upvotes: 0

Views: 55

Answers (3)

yassine
yassine

Reputation: 117

That's one of the downsides. There's for sure a way to do so, but without the iFrame.

You'll need to re-arrange the way your site or application is organised, there's better concepts to use.

But In the meantime, I think you can do what you want to do by getting the jQuery reference to your iFrame(child) from it's parent container like that :

var $= window.parent.$||window.parent.jQuery;

This reference getter variable is decalred inside your iFrame.

and between, just "Minify" all your resources. then of course you gain milliseconds or seconds why not of the total load time.

Upvotes: 1

Wally Farra
Wally Farra

Reputation: 13

Is any possibility to make another HTML file that contain all the common things that your two htmls have? That would be better and more organized, and more scalable for the future.

Upvotes: 0

Brent Echols
Brent Echols

Reputation: 590

I believe by design iframes don't have access to contents of its parent page, and the parent page doesn't have access to the iframe

jQuery/JavaScript: accessing contents of an iframe

Its a security feature to prevent spoofing a web site, and so it stops all cross-origin script interaction. If you are iframing in content from your own domain, than I would recommend rethinking how you manage your content.

Upvotes: 0

Related Questions