Welcome to WebmasterWorld Guest from 18.208.211.150

Forum Moderators: open

Message Too Old, No Replies

onLoad Timed Redirect

I wish it could be done with vbscript

     
6:06 pm on Jul 19, 2004 (gmt 0)

New User

10+ Year Member

joined:July 19, 2004
posts:4
votes: 0


Hi all

Something simple I suppose but I' m really stuck.
I want to redirect a page 5 secs after loading. The redirection target should be in the body as it is constructed dynamically.

THX

7:46 pm on July 19, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


Since we are in the Javascript forum:

<body onload="setTimeout('doRedirect()',5000)">
<script language="Javascript" type="text/javascript">
function doRedirect() {
//redirect to dynamic_variable after 5 seconds
top.location.href=dynamic_variable;
}
</script>
...yadda...
</body>

Grab your dynamic_variable location before the doRedirect script runs (within 5 seconds of page load).

7:50 pm on July 19, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


Is there some problem with writing the dynamic_variable to the HEAD? Why must it be in the BODY? What language/method are you using to generate the dynamic_variable? Pretty much all of the server-side languages (PHP, ASP, etc.) will let you write anywhere on the page.

For example:

<html>
<head><title>Some Title</title>
<?php $dynamic_variable="http://www.google.com";?>
<meta http-equiv="refresh" content="5; url=<?php echo $dynamic_variable;?>">
</head>
<body>
...yadda...
</body>
</html>

Will accomplish about the same thing as the Javascript version, above, without requiring Javascript.

7:37 am on July 20, 2004 (gmt 0)

New User

10+ Year Member

joined:July 19, 2004
posts:4
votes: 0


Thanks for your reply.

I've managed to solve the problem using the following:

<script language="JavaScript" type="text/JavaScript">

function doRedirect() {
window.location.href='done.asp?page=<% =request.QueryString("page")%>';
}

</script>
</head>
<body onLoad="setTimeout('doRedirect()',5000)">
</body>

8:28 pm on July 20, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


Why use Javascript for this?

<head>...
<meta http-equiv="refresh" content="5; url=done.asp?page=<% =request.QueryString("page")%>">
...
</head>

will do the redirect after 5 seconds, too...without relying on Javascript. It is true that some extra-paranoid people will have turned off the browser's ability to use the "refresh" meta tag, but there are lots more less-paranoid people who leave it on and turn off Javascript, instead.

Just thinking...

8:15 am on July 21, 2004 (gmt 0)

New User

10+ Year Member

joined:July 19, 2004
posts:4
votes: 0


The page that I' m developing is a automatic paging system that shows the next 20 records of a database.

The main reason that I choose to use javascript instead of a meta tag is that I wanted to make sure that the page will refresh 5 seconds after the whole page will be loaded.

If I used the refresh meta tag the 5 secs countdown will start right after loading the meta tag and not the body of the page. Consider that in many cases the page which displays 20 records needs more than 5 secs to load, so it is very possible that the page will refresh before loading all data.

4:49 pm on July 21, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


Okey dokey. Sounds like a good reason!

Amusingly, you could write the refresh meta tag line at the end of the display...it does not NEED to be in the head.

That way you could be absolutely certain that, no matter how long it took the data to display and no matter how the browser handled the body's onload attribute, your timer would start exactly when the display finishes.

Still just thinking...

(BTW I very much enjoy using Javascript, but if something definitely must happen, I try to find another way to trigger the event, as browsers' handling of Javascript is pretty much up to them.)