阻止 iframe 使用 javascript 加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:14:36  来源:igfitidea点击:

Stopping a iframe from loading a page using javascript

javascriptdomiframecomet

提问by Konrad

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.

javascript中有没有办法在加载页面的过程中停止iframe?我需要这样做的原因是我有一个来自 Web 服务器的后台 iframe 流数据(通过 Comet 样式机制),我需要能够随意切断连接。

Any ideas welcome.

欢迎任何想法。

回答by Chris Van Opstal

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

对于 FireFox/Safari/Chrome,您可以使用 window.stop():

window.frames[0].stop()

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

对于 IE,你可以用 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();
}

回答by Alex

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

整个代码应该是这样的,(unclenorton的那行少了一个括号)

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

回答by devside

Merely,

仅仅,

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

回答by CommonSenseCode

Very easy:

好简单:

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

1) 获取不想加载的 iframe 或 img:

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

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

2)然后你可以将 src 属性替换为 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:

如果您想在某个事件发生时一次加载 iframe 或图像,则只需将 src 变量存储在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

回答by Hashbrown

If you only have a reference to the element, you need to use .contentXto get the document/windowto run the accepted answer.
Checking that the iframe actually has a document is also necessary for dynamically added iframe elements.

如果您只有对元素的引用,则需要使用.contentX来获取document/window以运行已接受的答案。
检查 iframe 是否确实有文档对于动态添加的 iframe 元素也是必要的。

function stopIframe(element) {
    var doc = element.contentDocument;
    //iframes wont have a document if they aren't loading/loaded
    //check so JS wont throw
    if (!doc)
        return;

    //try for modern browsers
    if (doc.defaultView.stop)
        doc.defaultView.stop();
    //fallback for IE
    else
        doc.execCommand('Stop');
}

回答by Shakir K Moideen

not support in IE

IE 不支持

<script>
function stopit() {
window.stop();

};
</script>


<a href="#" onclick="stopit()" class="Stop" title="Stop"></a>