How can I stop an IFrame reloading when I change it's position in the DOM?

后端 未结 5 1954
鱼传尺愫
鱼传尺愫 2020-12-15 22:46

Is there any way to stop an Iframe re-loading it\'s contents when I change it\'s position within the DOM? Simple example:



        
相关标签:
5条回答
  • 2020-12-15 23:14

    Considering the simplicity of your test case, it looks like the only methods you have available to put an element inside another will always force the contents to reload.

    [Edit] After seeing what you're trying to do, there are a couple things you can try:

    1. All ads could be served in IFRAMEs (on your own site) which will not hold up loading of the page and can be placed in the right place right away.
    2. As mentioned above IFRAMEs won't hold up loading of the page so you could put the ads in place right away if they are IFRAMEs and load them at the bottom if they are something else. Of course, this won't work if the slow part is before you know if you are going to get an IFRAME or not.
    3. You could keep the content in it's placeholder DIV but when it's done loading just reposition (with CSS absolute positioning) over the right place in the page. Not the most elegant solution, but should work fine.
    0 讨论(0)
  • 2020-12-15 23:25

    What about using an ajax request to load the ad's contents, and adding the contents to the DOM when the ajax call returns, instead of using an iframe?

    0 讨论(0)
  • 2020-12-15 23:30

    I'm regretting my original answer, as it seems to be causing other headaches. Here's a few other potential solutions that you may not have tried:

    • Place the ad scripts inside of divs with their display style set to none. Then, move them to their final desintation and change them to display: block after the page has loaded. Perhaps this would prevent the iframes from loading their content inititially.
    • Try the same thing, only with visibility set to hidden, then changed to visible.
    0 讨论(0)
  • 2020-12-15 23:38

    A quick guess would be to unset the value of the src attribute of the iframe element or set it to "about:blank".

    It is up to you to restore the previous value (or any value) to the src attribute of the iframe (using JavaScript).

    Regards,

    0 讨论(0)
  • 2020-12-15 23:38

    If the ads are a fixed size, you could place them in absolutely-positioned divs instead. Then, once the page loads, you could move those container divs to their designated spots. There are a lot of Javascript samples out there for calculating an absolute position from a relative position. Of course, you would have to reserve space visually in the destination divs so the ads wouldn't cover the content.

    0 讨论(0)
提交回复
热议问题