Stopping a iframe from loading a page using javascript

Konrad picture Konrad · Feb 5, 2010 · Viewed 23.3k times · Source

Is there a way in javascript of stopping an iframe in the middle of loading a page? The reason I need to do this is I have a background iframe streaming data from a web server (via a Comet style mechanism) and I need to be able to sever the connection at will.

Any ideas welcome.

Answer

Chris Van Opstal picture Chris Van Opstal · Feb 5, 2010

For FireFox/Safari/Chrome you can use window.stop():

window.frames[0].stop()

For IE, you can do the same thing with document.execCommand('Stop'):

window.frames[0].document.execCommand('Stop')

For a cross-browser solution you could use:

if (navigator.appName == 'Microsoft Internet Explorer') {
  window.frames[0].document.execCommand('Stop');
} else {
  window.frames[0].stop();
}