0

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?

gespinha
  • 7,968
  • 16
  • 57
  • 91
  • leave just one script link in main HTML will NOT work – Steve Mar 07 '14 at 17:51
  • 1
    My understanding is that they are two documents, and both need the jQuery to be referenced independently. However, most browsers (AFAIK) will cache the jQuery file, so it should only need to be downloaded once – freefaller Mar 07 '14 at 17:51
  • There are many questions regarding optimization. Often from people whom seem to begin in web development. My advice, code your site as simple as possible, then once it works as expected, think about optimization – A. Wolff Mar 07 '14 at 17:53
  • The browser will only cache it if it refers to the same url, which it may or may not. jQuery is hosted on several different CDN's, so tis not guaranteed – Brent Echols Mar 07 '14 at 17:53
  • My assumption (and it an assumption) @Brent, is that the OP is talking about two pages within the same domain. If this is the case, there is a good chance it will be the exact same jQuery file – freefaller Mar 07 '14 at 17:54
  • @BrentEchols usually for a same website, all indentical scripts have same URL – A. Wolff Mar 07 '14 at 17:55
  • 1
    Well ya, if you are getting everything from the same domain it will be cached, but then that raises the question of why are you using an iframe on same domain. Just architect your code to be reusable. – Brent Echols Mar 07 '14 at 17:57
  • 1
    @Brent, if `iframes` are the best solution for the OP, then the OP should use `iframes`. Unless you know exactly what their system is doing / attempting to do, I'm not sure we're in a position to state what is the best. I regularly use iframes, especially within jQuery.dialogs, and that works very well for my needs. – freefaller Mar 07 '14 at 18:02

3 Answers3

1

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.

yassine
  • 117
  • 3
0

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.

Community
  • 1
  • 1
Brent Echols
  • 590
  • 2
  • 9
  • 2
    That is only the case if the parent and child pages come from different domains. If they're from the same domain, they can communicate to their little heart's content – freefaller Mar 07 '14 at 17:53
  • Ya, but my last comment stands that given its the same domain, just rearchitect your application. There are MUCH simpler ways to do things than just iframing in another portion of your app. – Brent Echols Mar 07 '14 at 17:54
0

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.

Wally Farra
  • 13
  • 1
  • 6
  • Why would this be better and more organised? This would only mean more server calls per load wouldn't it? – gespinha Mar 07 '14 at 17:56
  • 1
    Why server calls? I think that create just another simple html that includes all libraries you need to use in your project. So you will call the libs just once :) Or maybe Im not understanding your problem? :/ – Wally Farra Mar 07 '14 at 18:04