2 Pages V  1 2 >  
Reply to this topicStart new topic
> How to redirect visitors with JavaScript redirection

Moderator Alumni

Group Icon
Group: Hall Of Fame
Joined: 1-September 02
Posts: 9,213
From: UK
post Mar 12 2003, 10:50 PM
One of the most common uses for JavaScript is that of redirection, especially where frames are concerned. Unfortunately, the vast majority of JavaScript redirection is poorly done, breaking the browser back button. This simple 'How to...' tutorial shows you the correct code to use for javascript redirection.

JavaScript code for redirection is very widely available, but all too often it makes a classic mistake and so breaks the browser back button. This happens because when the user hits the back button, the browser goes back to where the javascript was (which is now cached and so works super-fast) which sends the browser instantly forward again.

The fault lies wherever you see the following code:
CODE
location.href=


Setting the property of the location.href to a value is a simple method for moving the user, but it means that the back button will take them back to the javascript.

Instead the correct code should use the location.replace(); method
CODE
location.replace(newurl);


The replace method actually does something pretty cool. It takes the current page location in the browser history and replaces it with the new url that you specify in the brackets. The old url that the code was on will no longer be in the browser history, and so the back button will go straight to the page before the one the script was on. Simple, effective and seamless.

Some Sample Uses:

To break free of framesets from other sites:
CODE
if (top != self) {

top.location.replace(self.location);

}


To reload the index page if the current page is not within your frameset:
CODE
if (top == self) {location.replace('index.htm');}


For more about dealing with Frames and Framed Sites see my article: SEO for Frames and Framed Sites
Offline Go to the top of the page

Untested

Group: Members
Joined: 29-March 03
Posts: 1
From: Guadalajara, México
post Mar 29 2003, 09:22 PM
:onfire:

Very useful tip! Thanks for sharing it. I've been developing websites for some time now, but I didn't know about location.replace();


ZAP
Offline Go to the top of the page

Moderator Alumni

Group Icon
Group: Hall Of Fame
Joined: 14-November 02
Posts: 7,197
From: Los Angeles
post Mar 30 2003, 12:55 AM
Hello, ZAP, and welcome to the forums!

BK, that's a great tip. It also explains what's happening with bbc.co.uk subpages ... tough to get back to the front page unless you click very fast.
Offline Go to the top of the page

Untested

Group: Members
Joined: 3-June 03
Posts: 1
post Jun 3 2003, 05:59 AM
Hi!
I must use a little javascript in my application and your example was very interesting for me.
In my case, the main window has three frames. And in the frame called "principal", I've got a comercial control we have bougth over the Internet. I think I have to use javascript like "location.replace (newURL)" when somebody clicking a button in that frame in order to redirect the user to another URL, but always in the same frame. I want to get access to the frame and change its URL. Does this sentence work?

window.frames('principal').location.replace (URL)

I've achieved it with window.parent.location.replace, but my goal is to get access to the frame, in order to have a more general function.

Am I accessing in the correct way to that property? Or have frames another property that lets me establish the new URL?????

Please help me, it's urgent!
Offline Go to the top of the page

Moderator Alumni

Group Icon
Group: Hall Of Fame
Joined: 1-September 02
Posts: 9,213
From: UK
post Jul 2 2003, 06:39 AM
No, making a page open within the correct frame is simply a case of using the TARGET attribute of the anchor tag.

If you have three frames, one called 'header', one called 'navbar' and one called 'main', then to make a link clicked in the 'navbar' frame open in your 'main' frame is simply as follows:
CODE
<a href="http://www.example.com/" target="main">Example Link</a>
Offline Go to the top of the page

Untested

Group: Members
Joined: 14-July 03
Posts: 1
post Jul 14 2003, 01:45 PM
urgent!

i have an index page with two frames (named 'menu' and 'frame') and a nav bar on the index page. i'd like the link button to open two urls at once in two different frames, OR open an url in one frame, 'menu' and on that page automatically open another page in the frame, 'frame'. is this possible?

..if you can understand what i just said.
Offline Go to the top of the page

Honorary Member

Group: Members
Joined: 16-December 02
Posts: 309
From: Bucks, UK
post Jul 14 2003, 05:03 PM
Welcome, wen! You can target multiple frames by giving your link anchor a JavaScript event:

Use this JavaScript event in the <head> of your document.

CODE
<script type="text/javascript">

<!--

function changeLink()

{

parent.menu.location.replace('page1.htm');

parent.otherframe.location.replace('page2.htm');

}

//-->

</script>

And create your menu links:

CODE
<a href="javascript:changeLink();">Link text</a>


However, you'll notice I've changed your second frame, originally named "frame" to "frameother". The word "frame" is a reserved word with JavaScript - it's actually part of the language's syntax, so you can't use it elsewhere. Try giving your frames (and everything else) names that won't clash with each other.

Hope this helps smile.gif
Offline Go to the top of the page

Member

Group: Members
Joined: 5-August 03
Posts: 17
From: West Palm Beach, Fl
post Aug 5 2003, 03:45 PM
Ammon, I've got a question. I'm finishing up my first site (I'm hoping to post it in the Hospital forum soon) and I've been reading a lot on SEO. I'm using a splash page (against my desire, the powers that be desire it - after being informed of the facts) and want to redirect to the 'top' page. I've heard that meta refresh is a definite no-no, and somewhere I heard that SE's didn't like javascript redirects either. So I had considered a small flash animation that would play though in a couple of seconds and then redirect users to the 'top' page. I'm hoping to make the flash splash page small, quick, with a skip and manual html text menu selections so hopefully roving bots can make the jump to the rest of the site. But I do want it set up so that users will be automatically redirected w/o an extra click.
Will the javascript you outlined cause SE penalties?

Brian
Offline Go to the top of the page

Moderator Alumni

Group Icon
Group: Hall Of Fame
Joined: 1-September 02
Posts: 9,213
From: UK
post Aug 5 2003, 07:21 PM
At least a couple of search engines are able to recognize the javascript used for redirection (either location.href= , or location.replace(); for example), and for a while they were notably downgraded.

The simplest solution is to use external javascript (a .JS file) to remove that code from the page, while still allowing it to work, and you can place the .js file within a cgi-bin or other scripts sub-directory of your site that is dissallowed in the robots.txt of your site.

These are not foolproof protections, of course, but if you ever find a search engine spider to be specifically requesting .js files, then run to the forums with the logs and let everyone know. It'll be big news.
Offline Go to the top of the page

Untested

Group: Members
Joined: 16-September 03
Posts: 2
From: Leeds
post Sep 17 2003, 08:05 PM
Hi All

Looking for a little help, or just my sanity back! :?

After two days (and nights!) of trying to get a javascript redirect to work I am coming to the conclusion that it will never work within Mac IE, both classic and OS X.

I am trying to achieve a very simple redirect, or more accuratly, use javascript to load two frames from a single click.

I have used the parent.FRAMENAME.location.replace("URL"); command and called the function through a javascript:nameLink() from a button. Every thing works fine in Safari, but only loads the SUBFRAME in IE.

I have not yet tested this on a PC version yet, so I have to say I am unsure wether it is just IE being its wonderful "i'm gonna do it my way" self or me being thick.

The site and code is available at www.realportraits.co.uk/index2.html if anyone has the time.

Thanks in advance, I'm going to grab those two nights back now. :shock:
Offline Go to the top of the page

Untested

Group: Members
Joined: 16-September 03
Posts: 2
From: Leeds
post Sep 18 2003, 04:00 PM
Solved it! biggrin.gif It was a problem with the FRAMESET and not the javascript. :oops:
Offline Go to the top of the page

Untested

Group: Members
Joined: 26-September 03
Posts: 2
post Sep 26 2003, 05:11 PM
Hello skilled fellows,
I'm no programming guru, and I need your help please....

I'm the owner of two domains, inmobiz.net and inmobiz.biz
I would like to redirect my visitors from inmobiz.net to inmobiz.biz
and solve the problem of confusing people..........

Problem is my domains are hosted on different webhosts.

What to do?? Please advice,
Thank you in advance,
Inga-Britt Molin
Offline Go to the top of the page

Member

Group: Members
Joined: 24-September 03
Posts: 25
post Sep 26 2003, 06:23 PM
Where did you register inmobiz.net? Some registrars let you use their nameservers for URL redirection. Maybe you should check with your registrar where in your login can you set your site to redirect. No script is required and you can save money on the hosting for inmobiz.net
Offline Go to the top of the page

Untested

Group: Members
Joined: 26-September 03
Posts: 2
post Sep 27 2003, 03:36 AM
Thank you liquid for your rapid answer!
I'll go have a look.
I registered both domains with GoDaddy.

I might come back for another stupid :oops:
question when I've solved this one biggrin.gif
inmobiz
Offline Go to the top of the page

Untested

Group: Members
Joined: 2-October 03
Posts: 1
post Oct 2 2003, 09:53 PM
Hi,

I have a website that the DNS is out of our hands at this moment and for a quick fix to redirect users to another domain will this work?

location.replace(newurl);

And is that all I need to place on the page for it to work?


<script type="text/javascript">
<!--
{
location.replace(newurl);
}
//-->
</script>

thanks in advance

seonut
Offline Go to the top of the page

Moderator Alumni

Group Icon
Group: Hall Of Fame
Joined: 1-September 02
Posts: 9,213
From: UK
post Oct 3 2003, 12:08 AM
Yes, that will redirect all users that support JavaScript (between 86-90 percent of all users as a rough figure). However, it is also detected by many search engines, and may cause the page the code is on to be dropped from the listings, since it is plainly a page you no longer want people to be on.
Offline Go to the top of the page

Member

Group: Members
Joined: 2-October 03
Posts: 10
From: philipines
post Oct 3 2003, 04:57 AM
help pls...!!!


my website working on http://www.mysite.com

but if i type http://mysite.com my default web page's doe's not display

someby help
[/img]
Offline Go to the top of the page

Member

Group: Members
Joined: 2-October 03
Posts: 10
From: philipines
post Oct 3 2003, 05:00 AM
i like them working both http and www

any could give me some idea where i'm mistake
Offline Go to the top of the page

Moderator Alumni

Group Icon
Group: Hall Of Fame
Joined: 1-September 02
Posts: 9,213
From: UK
post Oct 3 2003, 03:12 PM
The www is a subdomain, just a really really common one that almost everyone uses by default. Its not a matter of redirection, simply that your hosting hasn't set it up for you to use properly. Contact your hosting company.
Offline Go to the top of the page

Untested

Group: Members
Joined: 9-January 04
Posts: 2
post Jan 9 2004, 04:25 PM
I work for the Government of Canada and we often have numerous domain names active for the same site.

We'll set up something on the server at some point, but it often takes months for any change to go through...

As a temporary measure, we would like to force the use of a single domain for our users, but we want to keep the users on the page they were looking for.

We would like a JavaScript that will do the following:
* check if user is using domain.com or domaindev.com
* if not, reload the page they were viewing with domain.com

So, if they try to view wrongdomain.com/page.html, they will be redirected to domain.com/page.html.
Offline Go to the top of the page
Reply to this topic Start new topic
2 Pages V  1 2 >
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Jump to Forum:
 
Lo-Fi Version Time is now: 9th February 2010 - 05:56 PM
Meet our Moderators: cre8pc : projectphp : sanity : Black Phoenix : bwelford : EGOL : Ruud : rustybrick : AbleReach : swainzy : joedolson: eKstreme: dazzlindonna : SEOigloo: iamlost : RisaBB
Cre8asite RSS Feed