deltanovember
deltanovember

Reputation: 44091

How do I control the behavior of window.location in Javascript?

At the bottom of my page I have window.location = ...

I wish my page (let's call it jspage) to load, and then only once everything is fully loaded do the redirect. At the moment I get "stuck" on the referring page while jspage is processing, then jspage flashes up for a tiny instant and I am redirected

Upvotes: 0

Views: 188

Answers (2)

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

I would go with onload event of the window, and in there put small timer e.g. two seconds to ensure user will see the splash screen. Code for this would be simply:

<script type="text/javascript">
window.onload = function() {
   window.setTimeout(function() {
      document.location.href = "otherpage.html";
   }, 2000);
}
</script>

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

You can put your redirect to OnLoad handler of body, or use jQuery $(document).ready() to put your code in, or add timout and stay for some time on your jspage for better control of time when redirect happens.

But I'd start figuring out why you are "stuck" on referring page. It very well could be caused by slow server side processing of jspage rather than browser rendering (use Fiddler or Net tab of FireBug in Firefox to see when page actually comes back from server).

Upvotes: 4

Related Questions