How to redirect visitors with JavaScript redirection
#21
Posted 09 January 2004 - 05:38 PM
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
Posted 10 January 2004 - 09:23 AM
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
Posted 10 January 2004 - 01:04 PM
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
Posted 22 September 2004 - 02:13 AM
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
Posted 22 September 2004 - 04:04 AM
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
Posted 23 September 2004 - 05:11 AM
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
Posted 27 October 2005 - 01:25 PM
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
Posted 30 November 2005 - 07:18 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.
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
Posted 30 November 2005 - 07:24 AM
Why not do it server-side instead by editing your .htaccess file...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.
http://forums.digita...thread.php?t=13
#30
Posted 30 November 2005 - 08:09 AM
#31
Posted 30 November 2005 - 08:35 AM
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
Posted 26 January 2006 - 08:12 AM
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
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?
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
Posted 26 January 2006 - 09:30 AM
#35
Posted 26 January 2006 - 05:00 PM
#36
Posted 08 August 2006 - 04:07 AM
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
Posted 19 September 2006 - 02:06 AM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users







