Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping a iframe from loading a page using javascript

Tags:

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.

like image 929
Konrad Avatar asked Feb 05 '10 13:02

Konrad


People also ask

How do you stop an iframe?

There are two primary methods: 1.) Sending an X-Frame-Options HTTP response header that instructs the browser to disable framing from other domains. An example of using PHP to send the X-Frame-Options header.

Does iframe block page load?

Whereas iframes are typically used to include one HTML page within another, the Script in Iframe technique leverages them to load JavaScript without blocking, as shown by the Script in Iframe example.

How check iframe is loaded or not in JavaScript?

To check if iframe is loaded or it has a content with JavaScript, we can set the iframe's onload property to a function that runs when the iframe is loaded. document. querySelector("iframe"). onload = () => { console.


2 Answers

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(); } 
like image 200
Chris Van Opstal Avatar answered Dec 20 '22 05:12

Chris Van Opstal


The whole code should be like this, (unclenorton's line was missing a bracket)

if (typeof (window.frames[0].stop) === 'undefined'){     //Internet Explorer code     setTimeout(function() {window.frames[0].document.execCommand('Stop');},1000); }else{     //Other browsers     setTimeout(function() {window.frames[0].stop();},1000); } 
like image 26
Alex Avatar answered Dec 20 '22 05:12

Alex