Hidayat Ullah
Hidayat Ullah

Reputation: 157

How to check number of load time of a page in javascript?

In my scenario I,ve three pages in which one page load another page after 5 second duration, Now i want that when all the three pages loaded 200 time i want to stop loading of the pages further how can i achieve this using javasvript following is my code.

page1.html

 <html>
  <head>
   <title>Page1</title>
  </head>

<body>
<h1>Hello, This is page 1.</h1>
<div id="redirect" style="color: red;"></div>
    <p>page 1</p>
<script type="text/javascript">

function Redirect() 
{  

    window.location="page2.html"; 

} 

document.getElementById('redirect').innerHTML= " wait... You will be redirected to page 2 within 5 second" 
     setInterval('Redirect()', 5000); 
    </script>
   </body>
  </html>

Page2.html

<html>
 <head>
  <title>Page2</title>
 </head>

<body>
  <h1>Hello, This is page 2</h1>
  <div id="redirect" style="color:red;"></div>
    <p>page 2</p>
 <script type="text/javascript">
function Redirect() 
     {  
          window.location="page3.html"; 
     } 
 document.getElementById('redirect').innerHTML= "wait... You will be redirected page3 withing 5 seconds"; 
         setInterval('Redirect()',5000); 
      </script>
     </body>
    </html>

page3.html

 <html>
   <head>
   <title>Page3</title>
   </head>

     <body>
      <h1>Hi This is page 3.</h1>
       <div id="redirect" style="color:red;"></div>
      <p>page 3</p>
 <script type="text/javascript">
 function Redirect() 
      {  
        window.location="page1.html"; 
      } 
      document.getElementById('redirect').innerHTML = "You will be redirected to page 1 within 5 second."; 
setInterval('Redirect()', 5000); 
  </script>
 </body>
</html>

Upvotes: 0

Views: 830

Answers (3)

shema
shema

Reputation: 1

you can use $(window).load(function(){}) to set variable in local storage and check it every time window load

Upvotes: 0

Omar
Omar

Reputation: 452

Not sure what your end goal is but I bet you can keep track of the number of times each page loads by setting some sort of cookie var or sessionStorage in each page.

for example...

// Do this for each page. (add to bottom of each page, renaming the sessionStorage names for each).

// See if we have a pageLoadedCount value
if (sessionStorage.getItem("pageLoadedCount")) {
  var addPageLoadCount = sessionStorage.getItem("pageLoadedCount") + 1;
  sessionStorage.setItem("pageLoadedCount", addPageLoadCount);
}else{
  sessionStorage.setItem("pageLoadedCount", 1);
}

Upvotes: 1

YetAnotherBot
YetAnotherBot

Reputation: 2086

You should use local storage to store the count of loads.

On each page load you can check whether the load count has reached 200 or not.

If it has prevent the current page from reloading and set the load count to 0 again.

Upvotes: 0

Related Questions