Javascript 如何在html静态页面中停止页面加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8943219/
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
How to stop page load in html static page
提问by Eric Yin
Is there in HTML (javascript) or other static html tech can:
是否在 HTML (javascript) 或其他静态 html 技术中可以:
- Stop page loading (if browser does not download yet)
- Stop page rendering (from where the code placed)
- Stop javascript executed (from where the code placed)
- 停止页面加载(如果浏览器尚未下载)
- 停止页面渲染(从放置代码的位置)
- 停止 javascript 执行(从代码放置的位置)
Simply put, is there a code like
简单地说,有没有像这样的代码
<script>window.StopWhateverBelow()</script>
To let browser totally ignore whatever below the code.
让浏览器完全忽略代码下面的任何内容。
UPDATE
更新
I understand the browser may already download the whole thing. What I want is, from the code, page should stopped, for example: if I put the code just after <body>
visitor should see blank page, if I put the code in middle of the page, page should be just half like you pressed ESC
我知道浏览器可能已经下载了整个内容。我想要的是,从代码中,页面应该停止,例如:如果我把代码放在<body>
访问者应该看到空白页面之后,如果我把代码放在页面中间,页面应该只是你按下ESC 的一半
ANSWER
回答
As bukko suggested, comments done the trick. But not full, just half
If you put <!--
in html page, the rest will be ignored. And in Javascript
正如 bukko 所建议的,评论成功了。但是没有满,只有一半如果你放在<!--
html页面中,其余的将被忽略。在 Javascript 中
document.write('<!--');
Done the trick.
大功告成。
For about make sense:
关于有意义:
Here is how this code make sense: when you page load some offpage script, the script is dynamic. When the script code found something wrong (or match some condition), you have one more option to stop the page from rendering, loading, download...
以下是这段代码的意义:当您页面加载一些页外脚本时,该脚本是动态的。当脚本代码发现错误(或匹配某些条件)时,您还有一个选项可以阻止页面呈现、加载、下载...
采纳答案by chowey
If you already have html comments on your page, @bukko's solution doesn't fully work. Stuff after the html comment will get rendered normally.
如果您的页面上已经有 html 评论,@bukko 的解决方案不能完全奏效。html 注释之后的内容将正常呈现。
Something else to try is:
其他尝试是:
document.write('<script type="text/undefined">')
The rest of the document gets interpreted as part of the script tag, and because the script isn't text/javascript
, it gets ignored.
文档的其余部分被解释为脚本标记的一部分,并且因为脚本不是text/javascript
,所以它会被忽略。
Worked for me, thought I'd share it.
为我工作,以为我会分享它。
Edit
编辑
I've seen the script
trick not work in Chrome.
我已经看到这个script
技巧在 Chrome 中不起作用。
This does work, but I have not done extensive browser testing:
这确实有效,但我还没有进行广泛的浏览器测试:
document.write('<style type="text/undefined">')
回答by cyberboxster
You could do window.stop();
for most browsers besides Internet Explorer. For IE, I had to use document.execCommand('Stop');
window.stop();
除了 Internet Explorer 之外,您还可以为大多数浏览器执行此操作。对于 IE,我不得不使用document.execCommand('Stop');
回答by Kareem
window.stop(); //works in all browsers but IE
if ($.browser.msie) {document.execCommand("Stop");}; //works in IE,
document.execCommand works in IE, however it does stop some of FF, NS and some other browsers' functions. Like displaying GIF's animation for example. Using "if browser is IE" makes these two codes work perfectly in all browsers.
document.execCommand 在 IE 中工作,但是它确实停止了一些 FF、NS 和一些其他浏览器的功能。例如显示 GIF 的动画。使用“if browser is IE”使这两个代码在所有浏览器中都能完美运行。
回答by CompanyDroneFromSector7G
Well, there is:
<!--
terminated by
-->
for HTML, but scripts will ignore this.
嗯,有:
<!--
terminated by
-->
对于 HTML,但脚本将忽略这一点。
回答by Vicky
put window.Stop() wherever you want to stop the page from getting renderred
将 window.Stop() 放在您想要阻止页面渲染的任何位置
回答by Jakub
What you are asking makes no logical sense. Simply for two reasons:
你问的没有逻辑意义。简单的原因有两个:
- Data is ALREADY sent to the user (HTML / JS) so even tho if you COULD hide content, the data would sitll be there for a user to see (if they view source for instance).
- Why would you stop 'execution' of a page? It loads simple html structure and reults in a visual display, you should focus on the server site (php for instance) to hide or not send the content in the first place.
- 数据已经发送给用户(HTML / JS),因此即使您可以隐藏内容,数据也会在那里供用户查看(例如,如果他们查看源代码)。
- 为什么要停止“执行”页面?它加载简单的 html 结构并在视觉显示中显示结果,您应该首先关注服务器站点(例如 php)以隐藏或不发送内容。
If you want to visually hide elements tho, you could use CSS styles (hide divs or the like) with simply adding style="display:none;"
to a div like so:
如果您想在视觉上隐藏元素,您可以使用 CSS 样式(隐藏 div 等),只需添加style="display:none;"
到 div 中,如下所示:
<div style="display:none;">
This text will be downloaded by the user, but hidden from view due to CSS inline style
</div>
If you want to add commenting (thats just for your reference), then use comment formatting:
如果要添加注释(仅供参考),请使用注释格式:
<!-- this is a comment and will not show up to a user -->
Reference for comments: http://htmlhelp.com/reference/wilbur/misc/comment.html
评论参考:http: //htmlhelp.com/reference/wilbur/misc/comment.html
回答by Iain
HTML is static content so the server reads whatever you have written in the file unless you comment it out. For a dynamic file like what you are asking for you need to use php which can do this type of thing.
HTML 是静态内容,因此服务器会读取您在文件中写入的任何内容,除非您将其注释掉。对于像您要求的动态文件,您需要使用可以执行此类操作的 php。
回答by Bibaswann Bandyopadhyay
Just not much related to the question, but I thought it may be useful for some persons. If you want to jump to other page during page loading use window.location = "somepage.html";
or you can redirect users to the previous page: window.history.go(-1);
Useful in JavaScript conditional statements
只是与问题没有太大关系,但我认为它可能对某些人有用。如果你想在页面加载过程中跳转到其他页面使用window.location = "somepage.html";
或者你可以将用户重定向到上一页:window.history.go(-1);
在 JavaScript 条件语句中很有用
回答by pazndu prabhazwara
If you are using ASP or PHP, HTTP protocol automatically stops but HTTPS protocol don't stop automatically.
如果您使用的是 ASP 或 PHP,HTTP 协议会自动停止,但 HTTPS 协议不会自动停止。
To stop it use:
要停止它,请使用:
In ASP:
在 ASP 中:
dim r= accept.secure.protocol
r.onload=window.callback('c')
//to firefox,opera,safari
new clientObject(r).access()
// to chrome,ie
forEachElement(a==null);
PHP code:
PHP代码:
$a.window ;
All this scripts sends the browserstring "elementcast" by post method
所有这些脚本都通过 post 方法发送浏览器字符串“elementcast”
回答by Maciej Krawczyk
You could also hide the body like that:
你也可以像这样隐藏身体:
var style = document.createElement("style");
style.innerHTML="body { display:none !important; }";
document.getElementsByTagName("HEAD")[0].appendChild(style);