Jump to content

Leading Community for Usability, Search Engine Marketing,
Social Networking, Site Planning & Web Site Development, Since 1998


Photo

How to redirect visitors with JavaScript redirection


  • Please log in to reply
36 replies to this topic

#21 Black_Knight

Black_Knight

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 9293 posts
  • Twitter:http://twitter.com/#!/Ammon_Johns
  • Facebook:http://www.facebook.com/ammon.johns

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
  • 9293 posts
  • Twitter:http://twitter.com/#!/Ammon_Johns
  • Facebook:http://www.facebook.com/ammon.johns

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
  • 9293 posts
  • Twitter:http://twitter.com/#!/Ammon_Johns
  • Facebook:http://www.facebook.com/ammon.johns

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!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users