tags:

views:

39

answers:

2

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>";

?>
+1  A: 

Your code in PHP executes once and sends the content to the browser. When you refresh the page, the code doesn't run again in the server, because it is served by the cache. So the src of the iframe uses the same random number.

To avoid this you need to disable caching of the original page (not the iframe). Or you could have the random number generated in the client side (using javascript) so that is unique every time.

kgiannakakis
I already disabled the caching in the server as well as client side but it doesn't works. I saw the view source, the random id is generating properly each and everytime and the iframe src also changing properly but, the $_REQUEST object doesn't seems to be changed.
karuh24
+1  A: 

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;
      }
    }
});
Rajiv
hey Rajiv, your solution works perfectly. Thanks for the reply
karuh24