Forum Moderators: open

Message Too Old, No Replies

Redirect automatically to specified link in page <body>

Looking for a nice JS solution

         

MatthewHSE

1:43 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This concept is kind of hard to explain, so I'll just start by outlining a basic HTML page:

<html> 
<head>
<title>JS Redirect</title>
</head>
<body>
<a id="home" href="index.html">Home</a>
<a id="contact" href="contact.html">Contact</a>
<a id="support" href="support.html">Support</a>
</body>
</html>

Would there be any javascript I could put in the <head> of the page to select a link with a particular id and automatically redirect to it after the page has been loaded for five seconds or so?

Meta redirects and standard js redirects won't work; I've got to be able to select a link from one that's already in the <body> of the page.

I know this concept sounds wacky, but I really do have a good, "user-convenience" reason for doing this! ;)

Thanks,

Matthew

Bernard Marx

3:27 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you know beforehand which link you want redirected to?

MatthewHSE

3:54 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it will be the same id every time.

Bernard Marx

4:21 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..but the URL may change?

What I'm saying is that, if you know the URL in advance, then you can simply hard code it:

setTimeout("location='http://www.somewhere.com/blah.htm'",5000)

Do you mean that? Or is this for many pages, with same id, different href?

MatthewHSE

4:38 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, the id will always be the same but the href's will all be different.

DrDoc

4:50 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



location.href = document.getElementById('foobar').href;

Bernard Marx

5:28 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, precisely. Sorry, I was off trying to establish whether the replace method would accept relative URLs. It appears to.

So, yes, DrDoc's, or this below, if you would prefer the redirecting page not to reappear when the user goes back through history

location.replace(document.getElementById('foobar').href);

Come to think of it, referencing the link like this will save all of 1/2 a microsecond, and afford you a little backward-compatibility:

document.links['foobar']

MatthewHSE

5:45 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, I figured that getElementByID would come into play in this. But now, being a complete JavaScript know-nothing, I find I don't know how to use the scripts you two have so kindly provided. For instance, I tried this:

<html>

<head>

<title>Redirect</title>

<script type="text/javascript">location.replace(document.getElementById('foobar').href);</script>

</head>

<body>

<a id="foobar" href="blah.html">Blah</a>

</body>

</html>

Tried this with each of the lines you guys provided, but none of them redirected. Apparently I'm missing how to implement this. Could you please explain the context of how I'd go about using the script?

And then perhaps point me to a good, from-the-ground-up tutorial on JavaScript? ;)

Thanks again,

Matthew

DrDoc

5:47 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tried this with each of the lines you guys provided, but none of them redirected.

That's because thew JavaScript was executed before the page had fully loaded. You need to put the JS after the actual link element has loaded...

MatthewHSE

8:25 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, got it working! I am more convinced than ever now that I need to learn at least the basics of JavaScript. Do you know of any good free online tutorials?

Thanks again,

Matthew

Bernard Marx

9:29 pm on May 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are more than you can shake a stick at, but you have to watch out for the ones that are getting a little out of date (although they're still useful)

I see people recommend W3 Schools a lot

JavaScript: [w3schools.com ]
DHTML: [w3schools.com ]

For pure JavaScript:
[devedge.netscape.com ]

This cuts the corners to tell you things it might take a while to find out:
[crockford.com ]

This article will put you on the straight-and-narrow on an important topic:
[jibbering.com ]

Then return to WebmasterWorld JavaScript!

Rambo Tribble

2:13 am on May 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although it's far from free, I find O'Reilly's "JavaScript: The Definitive Guide" indispensible. The online tutorials I've seen have been a bit fragmented and superficial. This book is a scholarly treatise of JavaScript's full scope. Not light reading, but a substantial education in a single book.

Also of great utility is "Dynamic HTML: The Definitive Guide", also on O'Reilly. It's coverage of the DOM is the best I've seen, by a significant margin.

Bernard Marx

10:07 am on May 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, good to see how it's 'really' done.
There was a little about this here: [webmasterworld.com ]

j4mes

12:44 pm on May 22, 2004 (gmt 0)

10+ Year Member



W3Schools [w3schools.com] is always a good start, otherwise just Google for it and see what comes up!