admin管理员组

文章数量:1279120

Below is my code but page load continuously,i want to load only once

 window.onload = function () {
            window.location.reload();
        }

Below is my code but page load continuously,i want to load only once

 window.onload = function () {
            window.location.reload();
        }

Share Improve this question asked Apr 24, 2015 at 5:16 Ghanshyam LakhaniGhanshyam Lakhani 3724 silver badges21 bronze badges 2
  • 4 You are reloading the page whenever it is loaded. This is going to be a never ending loop – Vigneswaran Marimuthu Commented Apr 24, 2015 at 5:17
  • 3 Guys, stop downvoting the question. Being inpetent is not a crime. It's a good question in that it shows clearly what OP is doing wrong and says precisely what he wants to achieve. – Touffy Commented Apr 24, 2015 at 5:19
Add a ment  | 

5 Answers 5

Reset to default 4
$(document).ready(function(){    
    if(document.URL.indexOf("#")==-1){ //Check if the current URL contains '#'
        url = document.URL+"#"; // use "#". Add hash to URL
        location = "#";
        location.reload(true); //Reload the page
    }
});

Due to the if condition page will reload only once.

The other way to achieve this is :

(function()
{
  if( window.localStorage )
  {
    if( !localStorage.getItem('firstLoad') )
    {
      localStorage['firstLoad'] = true;
      window.location.reload();
    }  
    else
      localStorage.removeItem('firstLoad');
  }
})();

There are a few ways you could solve this, all of which require saving state across page loads. You could use cookies, localStorage, the location object itself, etc.

Here's a way that checks to see if there is a hash string 'reloaded' and, if not, adds it and reloads the page. Then, when it tries to execute again, the hash will be there and it will not reload:

if (location.hash.indexOf('reloaded') === -1) {
  location.hash += 'reloaded';
  location.reload();
}
window.onload = function () 
{
      // for getting params value
      function parse(val)
      {
        var  result = "not found";
          tmp = [];
          location.search
           .substr(1)
           .split("&") 
           .forEach(function (item) {
             tmp = item.split("=");
             if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
               });
          return result;
      }

      if(parse("load")!="once")
      {
        //sending parameter so next time it won't reload.. 
        window.location.href += "?load=once";
        window.location.reload();
       }
}

By nature of visiting a page, It will only load once. You could change your code to prove this fact:

window.onload = function () {
    alert("Loaded");
}

But, I would suggest the vapor.js route to detecting page load, that is, omit this onload call, because the lines of code in the onload function run after the page is loaded. I think you either don't know what your goal is or you have an entirely different problem you are trying to solve in a way that does not make sense

You built a loop,

  • site is loading
  • window.onload is triggered
  • reload is initiaded
  • site is (re-)loading
  • window.onload is triggered
  • reload is initiaded
  • .......
  • .......

Important fact for you to learn is that browsers run through your code from top to bottom and when you reload your page, the whole prozess repeats.

So every Time you reload, the window.onload event-listener is registered and calls the function attached to it, as soon as the window object is fully loaded.

There is no mechanism that tells the browser to stop.

if you would like run your Javascript code once the DOM is loaded, and you are looking for an browser independent solution i would remend jQuery and its $( document ).ready() function.

with jQuery included to your Page:

$( document ).ready(function(){
   //Code inside this function runs after your document is loaded
})

本文标签: javascriptHow to load page only onceStack Overflow