homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

How to redirect the visitor automatically without CGI ?
or Javascript ?

 2:07 pm on Jan 17, 2002 (gmt 0)

Is there a way to redirect without CGI or Javascript ?

Sorry for the dumb question :)

Thanks in advance




 2:11 pm on Jan 17, 2002 (gmt 0)

You can use a meta refresh tag in your html or alternatively .htaccess to send a redirect to the browser.


 2:15 pm on Jan 17, 2002 (gmt 0)

If you're using IIS you can set it up in the options for a particular domain or directory.

Right click on the directory in question (in IIS Manager through the MMC) and select the 'Home Directory' tab.

You'll notice an option for 'Redirect to URL' on the page. There are all sorts of settings you can apply to different things with this - they're all documented in the IIS help files.

I'm sure you can probably do similar things with non-IIS web servers.


 2:33 pm on Jan 17, 2002 (gmt 0)

To redirect using just HTML code, use a meta-tag like the following in the page <head>

<meta http-equiv="refresh" content="8;URL=http://...">

Personally, I prefer to use JavaScript. In case you are just shying away from it becaus you think it's too difficult, the JavaScript equivalent of the meta-tag above would be the following (also placed in the <head>):

<script language ="JavaScript">
setTimeout('window.location = "http://..."', '8000');

Note that the "8" and "8000" refer to the time lapse before the redirect takes place (the JavaScript number is in milliseconds).
Set this according to how many seconds you want to give someone to read the message on the original page, explaining that they are about to be redirected. (If it needs to be an immediate redirect without showing a "redirect page" set the number to 0.)

These are the main options if you want to handle it all on your HTML page. That makes sense if you want to display a "redirect page", but if you just want an automatic redirect, consider the server change mentioned in the previous post. (Another advantage to this is that you can change it all in one place, for a whole set of pages --an entire domain-- rather than having to add redirecting code in each page.)


 3:46 pm on Jan 17, 2002 (gmt 0)

bruhaha, why do your prefer js? any advantages over the metatag or is it just, as you say, personal?


 4:06 pm on Jan 17, 2002 (gmt 0)

I'm interested in bruhaha's answer too, in that the example above puts the javascript right on the page.

I also prefer javascript, but called from an external .js file. That way you run less chance of tripping some SE's misguided ideas of spam filtering.


 4:15 pm on Jan 17, 2002 (gmt 0)

Bare in mind that if you hide your redirects in JS then you run the risk of some SEs never following the redirect.

Tedster - Do you get around this by always having a normal link to the target on the redirecting page? Does this seem to work OK?


 4:23 pm on Jan 17, 2002 (gmt 0)

I haven't done any (well, many) redirects with JS before and it's got me interested now as I like tedsters point about spiders.

As I was just thinking about it: Shouldn't you really use the replace()* method for a redirect? This would prevent the user from 'bouncing' when they try to use the back button - one of the most annoying aspects of location = '..'?

I've just tried it in IE5, Opera6 and NN4 and it worked nicely.



 9:33 pm on Jan 17, 2002 (gmt 0)

Thanks everyone, I am going to go with :

<meta http-equiv="refresh" content="8;URL=http://...">

The reason I didn't want to use Javascript is because not all the visitors have it enabled.

One more question, I have put a NOINDEX, NOFOLLOW tag in this page so I assume NO search engine will penalise the page I am redirecting to ?




 10:07 pm on Jan 17, 2002 (gmt 0)

>> Shouldn't you really use the replace()* method for a redirect?

Absolutely. It's tailor made for this situation (and helps insures against visitor complaints to the SE as well.)

>> Do you get around this by always having a normal link to the target on the redirecting page? Does this seem to work OK?

It all depends on the reason for the redirect. Most of the time on my sites the spider would see essentially duplicate content. So I don't really want the spider to follow through.

If I did want the redirected page to be indexed then I would make sure had healthy linking from the page or elsewhere.


 7:22 am on Jan 18, 2002 (gmt 0)

Would someone please exemplify where replace()* is used in:

<script language ="JavaScript">
setTimeout('window.location = "http://..."', '8000');


 10:09 am on Jan 18, 2002 (gmt 0)

<script language ="JavaScript">
setTimeout('window.location.replace("http://...")', '8000');

Of course, if you don't want the page to hang around before redirecting (in the case above 8000 milliseconds) then you could just use:

<script language="JavaScript">


 3:11 pm on Jan 18, 2002 (gmt 0)

Might be good here to say a bit more about the replace() method. Browsers keep a history list of the pages recently visited. Using the replace() method, the current URL in the history list is replaced by the new one -- it's no longer there at all.

So, if you actually show your visitor a page before an automatic redirect, they might have reason to expect the Back Button will take them back to that page. I would only use replace() if I didn't expect to display the page at all.

If there's a Timeout of 8 seconds, you might consider which method is actually the best in your situation -- it can also be unintuitive to remove a page from the browser's History list that the visitor actually saw.


 3:26 pm on Jan 18, 2002 (gmt 0)

chiyo asks:
why do your prefer js?

Using the metatag may often work just as well as the javascript solution I suggested. But there are times where the added flexibility of javascript makes it preferable.

For instance, if you are concerned that the redirect page may take some time to download, and want to make sure it is fully downloaded before redirecting (if you fear people won't see the whole page before they are redirected), you can shift the javascript to "body onLoad". (I think, though, that redirect pages ought generally to be sparse in both text and graphics, so that download time should not be an issue.)

As for *location.replace, I agree that would certainly be preferable if the redirect is immediate --to save folk's getting caught in a loop when trying to go back, and not knowing what the problem is.

But I don't think this is so critical in the case of a delayed redirect, where they know what's happening. I prefer to leave them the option of backspacing to this redirect page, in case they happened to miss the explanation and wish to go back and check it out. If they can't get to that page--which location.replace would assure-- then they may be confused, and frustrated!

(The same logic seems to be operating with the metatag-refresh solution. If you set the time to '0', for an immediate refresh/redirect, the back button skips over it; anything longer allows you to go back to it.)

A factor to consider is the reason the redirect page is needed. Example: a company's name has changed signficantly, and so has the domain name of their corporate web site. Those looking for the company under the former name may be confused and unsure they are going to the right place. It can help them to be able to click back to the explanation if they miss it the first time.

In fact, this very thing recently happened to my company. And we placed a redirect page on the old domain with the javascript I mentioned above (and a clickable link for those who are not javascript enabled...or who are just impatient!)


 3:28 pm on Jan 18, 2002 (gmt 0)

Thanks, Tedster!
Just as while I was writing my reply you made my main point for me! (and much more succinctly!)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved