最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Reload an HTML page just once using JavaScript - Stack Overflow

programmeradmin1浏览0评论

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:

<script language=" JavaScript" ><!--
function MyReload()
{
window.location.reload();
}
//--></script>

<Body onLoad=" MyReload()" > 

The above code did not work for me.

However the code below is working well but the problem is that I need it to load only once:

<BODY BGCOLOR=#FFFFFF background="../images/Index_04.jpg" onLoad="history.go(0)" >

Note: I am using two iframes when user click on main page link a page loads in iframe then I want to reload the whole page with current iframe page.

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:

<script language=" JavaScript" ><!--
function MyReload()
{
window.location.reload();
}
//--></script>

<Body onLoad=" MyReload()" > 

The above code did not work for me.

However the code below is working well but the problem is that I need it to load only once:

<BODY BGCOLOR=#FFFFFF background="../images/Index_04.jpg" onLoad="history.go(0)" >

Note: I am using two iframes when user click on main page link a page loads in iframe then I want to reload the whole page with current iframe page.

Share Improve this question edited Mar 2, 2021 at 9:52 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 28, 2011 at 7:55 UsmanUsman 531 gold badge1 silver badge7 bronze badges 7
  • 1 Why the BOLD TAGS, the nineties are long gone – Ibu Commented May 28, 2011 at 7:57
  • you are tryin to reload everytime it loads, why are you trying to reload in the first way, maybe there is better alternative – Ibu Commented May 28, 2011 at 7:58
  • 2 This doesn't make any sense whatsoever, as soon as the page loads, it would reload and be a new page so it would reload again (for the first time for that load). I guess you are trying to get the browser to show the most recent content, that is done with cache control. – Quentin Commented May 28, 2011 at 7:58
  • 1 Can you explain why do you need to do that, so that our suggestions will be more in-context? – sergio Commented May 28, 2011 at 8:01
  • @lbu @Quentin read my question i need it loads once because i am using iframe when user click on link it loads a iframe than i want to load the whole with the same which he opens recently – Usman Commented May 28, 2011 at 8:01
 |  Show 2 more comments

4 Answers 4

Reset to default 6

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.

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:-

<script type="text/javascript">
   if(window.location.href.substr(-2) !== "?r") {
      window.location = window.location.href + "?r";
    }
</script>

That's it.

 <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.

Here is another solution which works for me using localStorage. In this solution we don't need to modify existing url.

<script type='text/javascript'>
 (function()
 {
  if( window.localStorage ){
    if(!localStorage.getItem('firstReLoad')){
     localStorage['firstReLoad'] = true;
     window.location.reload();
    } else {
     localStorage.removeItem('firstReLoad');
    }
  }
 })();
</script>

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.

发布评论

评论列表(0)

  1. 暂无评论