Note: Be careful using this if the page you use it on is listed in a search engine. Many of them find redirection to be a way of attempting to spam their index, and may remove the site from the listings. If you are unsure, don't try this-- or contact the search engine for their rules.
Redirection is often used to take viewers to a page depending on their browser's name or version. To redirect a viewer instantly, you just need to add a short command in your head section:
| <HEAD> <SCRIPT language="JavaScript"> <!-- window.location="http://someplace.com"; //--> </SCRIPT> </HEAD> |
This would take the viewer right to the url you used as soon as they start loading the page. Sometimes this is done if a site has moved to another location. Another use for it is to detect a browser type and redirect your viewers to one page for Netscape, another of Internet Explorer, or a third for other browsers:
| <HEAD> <SCRIPT language="JavaScript"> <!-- var browserName=navigator.appName; if (browserName=="Netscape") { window.location="http://www.someplace.com/ns.html"; } else { if (browserName=="Microsoft Internet Explorer") { window.location="http://www.someplace.com/ie.html"; } else { window.location="http://www.someplace.com/other.html"; } } //--> </SCRIPT> </HEAD> |
This uses the browser detection method from the previous section. Rather than popping up an alert box, we redirect the viewer to a page that best suits the browser being used.
If you want to have one page for version 4 browsers and another for others, we can take another script from the previous section and change it in the same way:
| <HEAD> <SCRIPT language="JavaScript"> <!-- var browserVer=parseInt(navigator.appVersion); if (browserVer >= 4) { window.location="http://www.someplace.com/v4.html"; } else { window.location="http://www.someplace.com/other.html"; } //--> </SCRIPT> </HEAD> |
Not too bad, the only trouble is the need to create so many pages!
You can also use this to help people who come into your site,but come in on a page that should be within a frameset that you use fornavigation. Using the top.frames.length object, you can find out if thepage is in a frame or not. If the value is zero, the page is not in aframe. If it is greater than zero, the page is inside a frame. So, youcould take the viewer back to your main frameset page using a scriptlike this:
| <HEAD> <SCRIPT language="JavaScript"> <!-- function getgoing() { top.location="http://someplace.com"; } if (top.frames.length==0) { alert("You will be redirected to our main page in 10 seconds!"); setTimeout('getgoing()',10000); } //--> </SCRIPT> </HEAD> |
This will alert the viewer and take them to the main page after 10 seconds. You can change the number in the setTimeout function to adjust the time if you like.
You can also use it to break a viewer out of someone else's frames if they followed a link that didn't have the target set correctly. It uses the same idea in reverse:
| <HEAD> <SCRIPT language="JavaScript"> <!-- function getgoing() { top.location="http://someplace.com"; } if (top.frames.length > 0) { alert("The frames will be broken in ten seconds."); setTimeout('getgoing()',10000); } //--> </SCRIPT> </HEAD> |
As you can see, redirection can be a handy tool at times. Just use it with caution if you are worried about the search engines.
Well, that does it for now. Let's go check out the next section, New Windows with JavaScript.
Related Articles
- JavaScript Browser DetectionBrowser detection allows you to find out what browser your viewer is using, and then perform a script based on it - or just to send a friendly message to those with your favorite browser.
- Create An Automatic Redirection PageMany novice webmasters launch their websites without having their own domain name or a commercial web host. Instead, they rely on free hosts, using a sub-domain (such as: yoursite.freehost.com or www.freehost.com/yoursite) as their websites URL.
However, as the number of visitors grows and thei.... - RedirectionHave you changed the navigation structure of your website by moving or deleting pages of your site? If a visitor has the old page bookmarked or comes from a link on another site to it, they will receive a 404 error; page can not be found. Solve this by redirecting the visitor to the new page. The me...
- JavaScript: Redirecting URLWhenever you want to generate dynamic pages you often have to resort to using some server side scripting language such as PHP or ASP
- The Basics- JavaScript TutorialTo get started with JavaScript, you will want to be able to see the tag that will set a script apart from the HTML. The tags used to begin and end a script are the tags. The opening tag should appear like this
- 4 Powerful Features of Javascript Programming LanguageIn the community of web developers and surfers, Javascript is highly popular as client side scripting language for web browsers. Lets take a look at some of the features of this language.
- JavaScript PromptsWell, lets say you wanted to get somebodys name before they saw the page, and then write their name on your page right before their very eyes... Well, you can do this using a javascript prompt. Heres the command:
- Parked Domain Names - Making Money with themHave you ever typed in a domain name and came to what looks like a default page of a domain name registrar that reads,
- Introduction to Javascript for NewbiesJavascript is a client-side programming language whose processing engine is embedded in web browsers like Internet Explorer, Netscape, Firefox, etc. This enables the processing engine to read and interpret web pages that contain the javascript code when browsing
- Form ValidationLearn the different ways of validating your form with Javascript.
