Jump to content

Cre8asiteforums Internet Marketing
and Conversion Web Design


Photo

How to redirect visitors with JavaScript redirection


  • Please log in to reply
36 replies to this topic

#1 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 12 March 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:
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
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:
if (top != self) {

top.location.replace(self.location);

}

To reload the index page if the current page is not within your frameset:
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

#2 ZAP

ZAP

    New To Community

  • Members
  • 1 posts

Posted 29 March 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

#3 DianeV

DianeV

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 7216 posts

Posted 30 March 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.

#4 lonifasiko

lonifasiko

    New To Community

  • Members
  • 1 posts

Posted 03 June 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!

#5 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 02 July 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:
<a href="http://www.example.com/" target="main">Example Link</a>


#6 wen

wen

    New To Community

  • Members
  • 1 posts

Posted 14 July 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.

#7 Nick

Nick

    Honorary Member

  • Members
  • 309 posts

Posted 14 July 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.

<script type="text/javascript">

<!--

function changeLink()

{

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

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

}

//-->

</script>
And create your menu links:

<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 :)

#8 Asher68

Asher68

    Ready To Fly Member

  • Members
  • 17 posts

Posted 05 August 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

#9 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 05 August 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.

#10 h133

h133

    New To Community

  • Members
  • 2 posts

Posted 17 September 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:

#11 h133

h133

    New To Community

  • Members
  • 2 posts

Posted 18 September 2003 - 04:00 PM

Solved it! :) It was a problem with the FRAMESET and not the javascript. :oops:

#12 inmobiz

inmobiz

    New To Community

  • Members
  • 2 posts

Posted 26 September 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

#13 liquid

liquid

    Ready To Fly Member

  • Members
  • 25 posts

Posted 26 September 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

#14 inmobiz

inmobiz

    New To Community

  • Members
  • 2 posts

Posted 27 September 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 :D
inmobiz

#15 seonut

seonut

    New To Community

  • Members
  • 1 posts

Posted 02 October 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

#16 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 03 October 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.

#17 pvclink

pvclink

    Ready To Fly Member

  • Members
  • 10 posts

Posted 03 October 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]

#18 pvclink

pvclink

    Ready To Fly Member

  • Members
  • 10 posts

Posted 03 October 2003 - 05:00 AM

i like them working both http and www

any could give me some idea where i'm mistake

#19 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 03 October 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.

#20 thxluke

thxluke

    New To Community

  • Members
  • 2 posts

Posted 09 January 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.

#21 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 09 January 2004 - 05:38 PM

In JavaScript, you'd be looking to modify the host object.

location.host

The host property of the location object is a little more rarely modified than the href property, but is handled in pretty much the same way.

#22 thxluke

thxluke

    New To Community

  • Members
  • 2 posts

Posted 10 January 2004 - 09:23 AM

Thanks!

Here's the code that works:
<script type="text/javascript" language="JavaScript">
var d = new String(window.location.host);
var p = new String(window.location.pathname);
var u = "http://" + d + p;
if ((u.indexOf("domain.gc.ca") == -1) && (u.indexOf("domaindev.com") == -1))
{
u = u.replace(location.host,"www.domain.gc.ca");
window.location = u;
}
</script>

#23 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 10 January 2004 - 01:04 PM

I would think that rather than breaking the url down into three separate variables, and then putting them back together, you could simply declare it once before doing your search and replace coding:

That means that
var d = new String(window.location.host); 

var p = new String(window.location.pathname); 

var u = "http://" + d + p;
can probably all be replaced with the simpler
var u = new String(window.location.href);
That way it is not only shorter code, but you'll also not be losing any referal info (if ever used) in a query string (location.search).

#24 jcba

jcba

    New To Community

  • Members
  • 2 posts

Posted 22 September 2004 - 02:13 AM

Hello Ammon

I have tried your method on my web site (with frames) in order to have a good behaviour of the back button.
When done, it works fine but I have seen a side effect :
when I clic the refresh button of my browser (tried with IE, Netscape & Opera) the page displayed in the main frame changes.
I have tried on your example (http://www.webmarket.../frameset1.html) and this behaviour is the same.

I explain :

I enter the following address in my browser :
http://www.webmarket.../content-2.html
Your Page 2 appears in the main frame , OK

Then I clic on the Page 1 link in the left menu
Your Page 1 appears in the main frame , OK

Now I clic the refresh button of my browser (or press F5)
in the main frame, your Page 1 desappears and your Page 2 appears instead.

So it seems that the page displayed after a refresh is always the first page that has been acces from the outside
(http://www.webmarket.../content-2.html in my example)

Is it a way to avoid this wrong behaviour of the refresh function ?

Thanks

#25 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9339 posts

Posted 22 September 2004 - 04:04 AM

Hi Jean-Claude, and welcome to Cre8asite :wave:

The article deals with this issue in having a separate frameset for each framed page (an ideal solution). That way, each time you chose a page to view, the frameset itself has changed too, and a refresh will show the correct page.

#26 jcba

jcba

    New To Community

  • Members
  • 2 posts

Posted 23 September 2004 - 05:11 AM

Hi Ammon,
Thank you for your quick answer.

If I understand,
for each page of my site xxxx.html, I have to create another page xxxx-frame.html and then in all my xxxx.html pages replace links to yyyy.html by links to yyyy-frame.html ?

Arrghhh ! A lot of job !

#27 Sathya

Sathya

    New To Community

  • Members
  • 1 posts

Posted 27 October 2005 - 01:25 PM

Hi,
Can we adjust the size of the frame conditionally? Actually i am creating a .NET Web page and after user selects some value, i have to display a web page in the bottom of the page conditionally only for specific values. for other values i dont have to show the web page.
Can you suggest me the best way to handle this scenario?
Thanks!

#28 kevingibbons

kevingibbons

    Ready To Fly Member

  • Members
  • 10 posts

Posted 30 November 2005 - 07:18 AM

Hi,

I am trying to redirect the website http://e-webdesign.co.uk to http://www.e-webdesign.co.uk so that there is no duplicate content picked up by search engines.

I have tried using the code above but this is not working:
<script type="text/javascript" language="JavaScript">
var d = new String(window.location.host);
var p = new String(window.location.pathname);
var u = "http://" + d + p;
if ((u.indexOf("e-webdesign.co.uk") == -1))
{
u = u.replace(location.host,"www.e-webdesign.co.uk");
window.location = u;
}
</script>

Thanks in advance,
Kevin

#29 SEbasic

SEbasic

    Ready To Fly Member

  • Members
  • 33 posts

Posted 30 November 2005 - 07:24 AM

I am trying to redirect the website http://e-webdesign.co.uk to http://www.e-webdesign.co.uk so that there is no duplicate content picked up by search engines.

Why not do it server-side instead by editing your .htaccess file...
http://forums.digita...thread.php?t=13

#30 kevingibbons

kevingibbons

    Ready To Fly Member

  • Members
  • 10 posts

Posted 30 November 2005 - 08:09 AM

I would do but my host doesn't allow .htaccess files on a windows server, thanks though

#31 JohnMu

JohnMu

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 3518 posts

Posted 30 November 2005 - 08:35 AM

Hi Kevin
If you use javascript, the search engines can still pick up the wrong domain! However, the users might not place "bad" links on their pages, so it's better than nothing. Also, keep in mind that some search engines don't like javascript redirects like that because they think it's a doorway page (etc).

If it is a windows server, you can do it in ASP though. I use the following code on my page:
<%

      if strcomp(Request.ServerVariables("SERVER_NAME"), "www.johannesmueller.com", vbCompareText)=0 then

               Response.Clear

               Response.Status ="301 Moved Permanently"

               strNewUrl = Request.ServerVariables("URL") 

               if Request.QueryString <> "" then

                   Response.AddHeader "Location","http://johannesmueller.com" & strNewUrl & "?" & Request.QueryString

               else

                   Response.AddHeader "Location","http://johannesmueller.com" & strNewUrl

               end if

               Response.End

        end if

%>

which I have on all my pages, it even redirects any querystring that might be attached (never used it though). It redirects from www to the non-www version, but you can turn it around just as well.

John

#32 mishoo

mishoo

    New To Community

  • Members
  • 2 posts

Posted 26 January 2006 - 08:12 AM

hello everybody!

i have a small problem with javascript redirect. i use on my website a button to redirect people :

<input type="button" onclick="javascript:top.location.replace('newpage.html')" value="go">

on the newpage.html a have a button back :

<input type="button" onclick="javascript:window.history.back()" value="back">

when i push the second button the browser to the one previously of the one with the first button. do you know what could go wrong?

10x for your help!

Edited by mishoo, 26 January 2006 - 08:12 AM.


#33 Tim

Tim

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 3094 posts

Posted 26 January 2006 - 08:26 AM

when i push the second button the browser to the one previously of the one with the first button. do you know what could go wrong?

View Post


This is happening because the code is replacing the location, not creating a new location. Therefore, no new history item is created, so going back simply goes back to the page that was replaced.

So, what you need to do in this instance is use location.href='' instead of location.replace(''). location.replace should be used when you are performing an automatic redirect (because without it the back button will just send the visitor forward again after sending them back), but if the redirect is invoked by the visitor performing an action (in this case, clicking a button), then location.href should be used.

Your code needs to change to this:

<input type="button" onclick="javascript:top.location.href='newpage.html';" value="go">


#34 mishoo

mishoo

    New To Community

  • Members
  • 2 posts

Posted 26 January 2006 - 09:30 AM

10x a lot!!! that worked just fine

#35 Tim

Tim

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 3094 posts

Posted 26 January 2006 - 05:00 PM

You're welcome. Glad to be able to help. :)

#36 digitalecartonos

digitalecartonos

    New To Community

  • Members
  • 1 posts

Posted 08 August 2006 - 04:07 AM

I've used the following AS in my Flash navigational buttons to load the 5 individual contentpages in the content frame of my frameset named "content"

getURL("http://www.digitalec...ns.nl/mail.htm", "content");

This works ok.

In case someone types www.digitalecartoons.nl/mail.htm, I didn't want the mail.htm page to be displayed. Without the navigational frame. I solved this with the following javascript in each of the 5 pages, so that instead the homepage is loaded.

<script TYPE="text/javascript">
<!--
if (window == top) {
top.location.replace("index.htm"); } //-->
</SCRIPT>

This also works ok.

But now I get an email from somebody saying that when she clicks on any flash button, instead of the page being loaded into the "contents" frame of the index frameset, the homepage is reloaded. As if in here case the javascript doesn't recognize the "content" part in the geturl. Or as of the Flash button doesn't use the "content" part.

I haven't heard any complaints like this, but what could be wrong in her case?

#37 Jojo

Jojo

    New To Community

  • Members
  • 1 posts

Posted 19 September 2006 - 02:06 AM

Hi all, I am a new member here. Just wanted to thank NICK for his contributions cuz they helped solved many of my headaches... Thanks and please keep up the good work everyone. You guys rock!!



RSS Feed

2 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users


    Bing (1)