iframe reload caching issue

2019-08-13 03:37发布

问题:

I am executing the following code in an iFrame:

window.frames[0].location.reload();

It reloads the page, however, it looks like SOMETIMES the data is cached. I found the source of the URL that it is reloading. What I would like to do is append a random number to the URL so that when it reloads it is unique and will prevent caching.

How would I do that with the above code?

UPDATE:

I have modified the code where I'm attempting to set the href to a new URL string that I built. However, it looks like when I print out the location.href after setting it (the line above), I'm still getting the old location.href.

            window.frames[0].location.href = newIfURL;
            console.log(window.frames[0].location.href);
            window.frames[0].location.href();

Am I doing this right?

回答1:

Something like:

var fl = window.frames[0].location;
var href = fl.protocol + '//' + fl.host + fl.pathname + '?random=' + Math.random() + fl.hash;
window.frames[0].location.href = href;

Of course, if you need to preserve "search" parameters, then you will need to add some logic to determine how to add the random parameter.