使用 JavaScript 重新加载 HTML 页面一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6160415/
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
Reload an HTML page just once using JavaScript
提问by Usman
How can I reload an HTML base web page only once? I am using history.go(0);
with function onLoad
in body tag but i want to run it only once. Please keep in mind that i am using iframe
so this is not possible to to use the below types code:
如何仅重新加载 HTML 基础网页一次?我在 body 标签中使用history.go(0);
with 函数onLoad
,但我只想运行它一次。请记住,我正在使用,iframe
因此无法使用以下类型代码:
<script language=" JavaScript" ><!--
function MyReload()
{
window.location.reload();
}
//--></script>
<Body onLoad=" MyReload()" >
the above code not worked for me
上面的代码对我不起作用
but the below code is work good but the problem is that i need it to loads once
但下面的代码运行良好,但问题是我需要它加载一次
<BODY BGCOLOR=#FFFFFF background="../images/Index_04.jpg" onLoad="history.go(0)" >
note: I am using 2 iframes when user click on main page link a page loads in iframe than i want to reload the whole page with current iframe page.
注意:当用户单击主页链接时,我使用了 2 个 iframe,一个页面在 iframe 中加载,而不是我想用当前 iframe 页面重新加载整个页面。
回答by isNaN1247
You could use a querystring
at the end of the page url (e.g. ?r), and check for it before redirecting, as if it exists, the redirect is already done.
您可以querystring
在页面 url 的末尾使用 a (例如 ?r),并在重定向之前检查它,如果它存在,则重定向已经完成。
if(window.location.href.substr(-2) !== "?r") {
window.location = window.location.href + "?r";
}
Disclaimer: I agree with others that you probably have a better solution - and should never need to refresh 'just once'.
免责声明:我同意其他人的看法,您可能有更好的解决方案 - 并且永远不需要“只刷新一次”。
Explanation of use
使用说明
At the bottom of your page, just above the </body>
you should put this:-
在您的页面底部,就在上面,</body>
您应该放置以下内容:-
<script type="text/javascript">
if(window.location.href.substr(-2) !== "?r") {
window.location = window.location.href + "?r";
}
</script>
That's it.
就是这样。
回答by Parag Gaiwkad
<script type="text/javascript">
$(document).ready(function(){
//Check if the current URL contains '#'
if(document.URL.indexOf("#")==-1)
{
// Set the URL to whatever it was plus "#".
url = document.URL+"#";
location = "#";
//Reload the page
location.reload(true);
}
});
</script>
Due to the if condition the page will reload only once. Hope this will help.
由于 if 条件,页面只会重新加载一次。希望这会有所帮助。
回答by NiRmaL
Here is another solution which works for me using localStorage. In this solution we don't need to modify existing url.
这是另一个使用 localStorage 对我有用的解决方案。在这个解决方案中,我们不需要修改现有的 url。
<script type='text/javascript'>
(function()
{
if( window.localStorage ){
if(!localStorage.getItem('firstReLoad')){
localStorage['firstReLoad'] = true;
window.location.reload();
} else {
localStorage.removeItem('firstReLoad');
}
}
})();
</script>
回答by Itai Sagi
The only way I can think of to do this is by using cookies (or if your on the right platform, sessions) and flag the first time it has reloaded, variables might also work in this case, because we're speaking about IFRAME, but I have never tried such thing.
我能想到的唯一方法是使用 cookie(或者如果您在正确的平台上,会话)并在它第一次重新加载时进行标记,变量在这种情况下也可能起作用,因为我们正在谈论 IFRAME,但我从来没有尝试过这样的事情。