-1

I have javascript page that calling another page. when the second page is called, I send request data to sql server to see how many time the second page is called. But always see that just once. I've added dynamic querystring to prevent cache from browser this is my code:

for(i=1;i<=30;i++)
 {
  var q="eeee?id=" + i;
  window.location=q;
 }

But alwayes see these 2 records in database instead of 31 records:

javacall ==> first page that has java call function

eeee?id=30

why page is called in 30th call and not from 1 to 30, for example:

javacall

eeee?id=1

.

.

.

eeee?id=30

Reza Akraminejad
  • 1,412
  • 3
  • 24
  • 38

1 Answers1

0

The browser doesn't check to see if location has changed until the JavaScript has stopped running (it is too busy running the JavaScript until then).

When the loop gets to the end, it will be set to the last value. At the point, the browser registers that it has changed and loads the new URL.

If that wasn't the case, then the first change to it would cause the browser to leave the current page… which will destroy the execution environment the script was running it, abort the script and cause the subsequent URLs to not be requested.

If you want to make multiple HTTP requests from JavaScript, then use Ajax. You'd normally do this through the XMLHttpRequest object.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335