Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iframe src caching issue on firefox

Tags:

iframe

I have an iframe element with a random scr attribute. When I do refresh the page every time, the iframe should load the page with different query parameters based on the src attribute. But in firefox, if I try to load dynamic URL in an iframe, it always execute the first time executed URL eventhough the src attribute changes dynamically. The query parameters also not passing correctly. So, how I can solve this issue?

eg:

<?php

$url = "http://localhost/test.php";

$rand_val = rand(1000, 9999);

echo "<iframe name='dynamicload' src='{$url}?rand_val={$rand_val}'></iframe>";

?>
like image 659
karuh24 Avatar asked Jul 15 '10 07:07

karuh24


2 Answers

We had the same problem with firefox caching the iframe src and disabling the cache on the original page as well as the iframe page did not help. We put the following code (jQuery code) in the onload function of iframe:

$(parent.document).find("iframe").each(function() {
    // apply the logic only to the current iframe only
    if(this.contentDocument == window.document) {
       // if the href of the iframe is not same as 
       // the value of src attribute then reload it
      if(this.src != location.href) {
        this.src = this.src;
      }
    }
});
like image 184
Rajiv Avatar answered Oct 21 '22 13:10

Rajiv


It's reported as a bug of firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=279048

one workaround is resetting the src of iframe: document.getElementById('iframe_id').src = 'target_url';

Still there will be two requests: the first request is wrong and cancelled immediately before the second request which is correct.

like image 31
user555654 Avatar answered Oct 21 '22 15:10

user555654