Javascript 停止加载 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8961455/
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-24 08:12:53  来源:igfitidea点击:

Stop iframe from loading

javascriptjqueryhtml

提问by Linas

So i have iframe and i'm uploading files through it, so my question is how can i stop it in the middle of the loading? I've tried to change src with jquery function attr()but i didn't do any good, i'm thinking of removing the whole iframe with jsbut it would create me other problems and i'm not even sure if it would work. So is there some other ways to do it?

所以我有 iframe 并且我正在通过它上传文件,所以我的问题是如何在加载过程中停止它?我试图用 jquery 函数更改 src,attr()但我没有做任何好事,我正在考虑删除整个 iframe,js但这会给我带来其他问题,我什至不确定它是否有效。那么还有其他方法可以做到吗?

My next question would be, is it possible to hide loading indicators so it wouldn't confuse users that page is loading or something.

我的下一个问题是,是否可以隐藏加载指示符,以免用户混淆页面正在加载或其他内容。

Answers like you should use flash or other stuff won't be accepted i already did everything with iframe i just need to do those 2 things.

像你应该使用 flash 或其他东西这样的答案不会被接受我已经用 iframe 做了所有的事情,我只需要做这两件事。

Here is my iframe code:

这是我的 iframe 代码:

<iframe onload="file_uploaded()" class="iframe" src="user/upload_image/" name="iframeTarget" ></iframe>

回答by Rick Kuipers

I believe the following answers your question: https://stackoverflow.com/a/2207589/1157493

我相信以下回答你的问题:https: //stackoverflow.com/a/2207589/1157493

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

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

window.frames[0].stop()

对于 IE,你可以用 document.execCommand('Stop') 做同样的事情:

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

对于跨浏览器解决方案,您可以使用:

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

If you'd like the iframe to go to a different page you could try to redirect it to "about:blank"

如果您希望 iframe 转到其他页面,您可以尝试将其重定向到“about:blank”

回答by polach.o

This works for me (IE, Chrome, FF, Opera):

这对我有用(IE、Chrome、FF、Opera):

$iframe.attr('src','about:blank');

回答by Mehdi Zamani

can use this for disabling all iframe content

可以使用它来禁用所有 iframe 内容

<script>
    var frames = window.frames;
    var i;

    for (i = 0; i < frames.length; i++) {
        frames[i].location = "";
    }
</script>