View Full Version : Javascript reload on iframe gets permission denied error

03-21-2012, 04:06 AM
Trying to use js to reload an iframe so that it can do a query again and show a live update to the given number.

I'm doing:
<iframe id="ifrm" name="ifrm" onload="ifrm.window.location.reload();" src="url" width="400px" height="100px" scrolling="no" frameborder="0">
Sorry, your browser doesn't support iframes.

Any ideas?

Old Pedant
03-21-2012, 05:03 AM
No, that won't work. For starters, only MSIE lets you refer to objects by their id directly, as you are doing.

Other browsers would require document.getElementById('ifrm')

But why do either? Just use this to refer to the iframe. After all, the onload method is a method on <iframe>.

But I don't get why you want the <iframe> to be reloaded as soon as it is loaded. Wouldn't you want to wait a while, for something to change? As it is, you are virtually guaranteed that you will be reloading exactly what was just loaded.

Old Pedant
03-21-2012, 05:07 AM
Oh, and the reason you are getting the permission denied error: Apparently the URL in the <iframe> is from a different server than the URL of the main window. So by trying to use JavaScript that runs in the main window to invoke the reload method of the <iframe>, you are doing cross-site scripting, which is disallowed.

Why not simply change src= of the <iframe>? That you *can* do from the main window.

e.g., document.getElementById('ifrm').src='http://www.anothersite.com?zzzzz='+Math.random();

Tacking on the random querystring will force the browser to load the page instead of using a cached copy.

03-21-2012, 11:22 PM
Hi Old Pendant, you are right about the cross website security issue, there is no way to change content hosted on a different url.