Stopping a iframe from loading a page using javascript

24,940

Solution 1

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();
}

Solution 2

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);
}

Solution 3

Merely,

document.getElementById("myiframe").src = '';

Solution 4

Very easy:

1) Get the iframe or img you don't want to load:

let myIframe = document.getElementById('my-iframe')

2) Then you can just replace src attribute to about.blank:

myIframe.src = 'about:blank'

That's all.


If you wanted to load the iframe or image at a time in feature when some event happens then just store the src variable in dataset:

myIframe.dataset.srcBackup = myIframe.src
// then replace by about blank
myIframe.src = 'about:blank'

Now you can use it when needed easily:

myIframe.src = myIframe.dataset.srcBackup
Share:
24,940

Related videos on Youtube

uss
Author by

uss

About me.. check out my blog. :)

Updated on June 26, 2020

Comments

  • uss
    uss almost 4 years

    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.

    • Richard JP Le Guen
      Richard JP Le Guen about 14 years
      Not confident enough to put it as an answer, but couldn't you just direct the iframe to another url? Something like document.getElementById("myiframe").src = "http://www.example.com/not-long-polling.html";
    • uss
      uss about 14 years
      That was my first idea (+1), but I need the contents of the frame to be preserved.
  • unclenorton
    unclenorton almost 13 years
    It is probably better to check for typeof(window.frames[0].stop) instead of navigator.appName. Future IE version may support that.
  • homerun
    homerun about 12 years
    @unclenorton i don't really get what you trying to say. you may write the condition ?
  • unclenorton
    unclenorton about 12 years
    @MorSela, I am trying to say that it is better to check for a feature rather than for a browser. The condition will look like (typeof (window.frames[0].stop === 'undefined').
  • ghusse
    ghusse about 12 years
    Thanks for this answer. I agree with unclenorton : it's better to test the presence of a feature itself instead of testing the browser.
  • Steve Wasiura
    Steve Wasiura almost 12 years
    Alex's example using typeof() works pefectly. upvoting that answer for others to find faster
  • v42
    v42 almost 12 years
    For those who are using jQuery, i had problems while testing the stop function, because of the jQuery method with the same name. To solve this, I used javascript dom selectors like this: document.getElementById("myId").getElementsByTagName('iframe‌​')[0]. I don't know if there's an easy alternative for this, but it does the job.
  • Marged
    Marged almost 9 years
    Please have a look at the accepted answer, I think it is quite equal to what you posted
  • Nishi
    Nishi about 8 years
    Why is setTimeout used?
  • Dennis Rosenbaum
    Dennis Rosenbaum almost 7 years
    As addition to @v42, after getting the frame, you have to target the contentWindow: var iframe = document.getElementById("myId").getElementsByTagName('iframe‌​‌​')[0].contentWindo‌​w;
  • Admin
    Admin almost 6 years
    That doesn't stop large and heavy javascript execution, like Unity WebGL Build.