Forum Moderators: open

Message Too Old, No Replies

redirect in <body>

need to redirect but execute some code first

         

HelenDev

12:24 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to place a javascript redirect in the body of a page?

I have some js code in the body of the page which I need to execute before doing a redirect, like this

<html>
<head>
</head>
<body>
//js code here for stats tag
//need redirect to happen at this point
</body>
</html>

Is this possible?

kaled

2:10 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Simply use
location.href='newpage.html';
or
location.replace('newpage.html');

The latter will cause the intermediate page to be omitted from the history list for the window.

If you explain the whole problem, someone may have a better solution.

Kaled.

HelenDev

2:42 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers Kaled,

putting a normal redirect in does work insofar as the page will redirect, but my statscript doesn't appear to be activated first. Does putting the redirect in cause the code which comes first to be ignored?


<HTML>
<body stats=1>
<script language="javascript">
<!-- code for my StatScript here -->
window.location='http://pub.exeter.gov.uk/asp/eahod/';
</script>
</BODY>
</HTML>

rocknbil

3:42 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may be executing, just too quickly to see it. Try using a timeout, in thousandths of seconds. If your stats are from another server, this may work or not depending on how long it takes to connect. If local, a second or two second timeout should work.

<script language="javascript">
setTimeout('location.href="http://example.com"',2000);
</script>

Or use location.replace, window.location=url, etc.

EDIT: note the url is double quoted inside single quotes:

setTimeout(' location.href= "url" ',5000);

HelenDev

4:07 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers rocknbil.

Just had a reply from my stats provider who recommended the exact same thing. Great minds and all that... :)

I guess I'll have to go with this. I'm a little concerned about doing this kind of stuff with js and delays etc. it doesn't seem like the cleanest way of doing stuff, but it seems the only way to track things with my stats package.

Cheers,
Helen.

rocknbil

4:39 pm on May 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


Don't want to use JS?

<meta http-equiv="refresh" content="2; url=http://www.example.com">

Would redirect after two seconds.

Don't you have some way of registering that your stats script is being triggered? Not knowing what it's doing, it seems like you could send some unique variable or marker from this page, then go check the data and see if it picked it up. As I already guessed, it may be working fine just too quickly to see it.

Rambo Tribble

5:50 pm on May 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the stats script submitting a form? If so, it may be that action which isn't taken to completion, not the script initiating it.

HelenDev

2:33 pm on May 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't you have some way of registering that your stats script is being triggered?

only that the page shows up in the stats report, or doesn't.

Is the stats script submitting a form?

I'm not sure exactly what the stats script does, but I understand that it tracks an image, and it uses cookies. I don't think forms are involved.

Cheers for your feedback anyway guys :) as it appears to be working OK at the moment, so I'm gonna leave well enough alone, even with the js delayed redirects.

The site is going live in a few days and I don't want to break it!

Rambo Tribble

10:35 pm on May 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suspect the problem with redirect might be occurring because the image used for tracking doesn't have a chance to load before the redirect fires. You could try giving the meta tag refresh a longer than necessary timeout before execution (to allow for those with JS disabled) and trigger your JS location.href redirect with the onload event.