Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

JavaScript Redirection

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

ivenms
Administrator
*******


Posts: 2,179
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4389 (Donate)
Post: #1

Rolleyes JavaScript Redirection


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 for navigation. Using the top.frames.length object, you can find out if the page is in a frame or not. If the value is zero, the page is not in a frame. If it is greater than zero, the page is inside a frame. So, you could take the viewer back to your main frameset page using a script like 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.


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
15-09-2006 03:21 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
Smile Learn JavaScript Basics ivenms 1 677 14-07-2008 06:20 PM
Last Post: ratedguy
Toungue Image Rollover Script Javascript ratedguy 0 79 14-07-2008 06:15 PM
Last Post: ratedguy
  I'm New to Javascript MaGiC DaN90 3 917 16-05-2008 04:37 PM
Last Post: ivenms
  Using iframes with javascript godkillah 0 1,600 26-01-2008 06:19 AM
Last Post: godkillah
  The JavaScript that Randomly Select the Quotes ivenms 9 645 29-09-2007 01:46 AM
Last Post: godkillah
Big Grin JavaScript Browser Detection ivenms 0 829 15-09-2006 03:20 AM
Last Post: ivenms
Smile JavaScript Alerts ivenms 0 650 15-09-2006 03:04 AM
Last Post: ivenms

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 13-10-2008, 10:59 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB