...

View Full Version : JavaScript redirection code not working



debi5767
09-02-2011, 02:32 PM
Hi

I am using some JavaScript redirection code (You can find the code bellow) for my website
Some months before everything was all right. But I donít know what happened now
The code is not working

Can anyone help me Ė I am very new to javascript

Thanks



<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>

<script language="JavaScript1.2"><!--

// The following only works in JavaScript 1.2 or greater:
function showbelgium()

{
top.location.href = 'http://www.example.com/en/belgium.html';

}
function showluxembourg()

{

top.location.href = 'http://www.example.com/en/luxembourg.html';

}

function showmorocco()

{

top.location.href = 'http://www.example.com/en/morocco.html';

}

function shownetherlands()

{

top.location.href = 'http://www.example.com/en/thenetherlands.html';

}

function showsingapore()

{

top.location.href = 'http://www.example.com/en/singapore.html';

}

function showuk()

{

top.location.href = 'http://www.example.com/en/uk.html';

}

function showus()

{

top.location.href = ' http://www.example.com/en/usa.html';

}

function showrestofworld()

{

top.location.href = 'http://www.example.com/en'

}

var language = geoip_country_code();

var code = language.substring(0,2);


if(code == 'BE')

showbelgium();

else if(code == 'LU')

showluxembourg();

else if(code == 'MA')

showmorocco();

else if(code == 'NL')

shownetherlands();

else if(code == 'SG')

showsingapore();

else if(code == 'GB')

showuk();

else if(code == 'US')

showus();

else

showrestofworld();

</script>

jmrker
09-02-2011, 05:52 PM
The following is untested code, but it does simplify you multiple if-else statements
(which I would always recommend putting between { } pairs
See: http://www.codingforums.com/showthread.php?t=236556 post #14).

I'm also not sure the "top.location.href=" is correct.
I have always used "document.location.href=" in the past,
but if it works for you then go for it!



<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>

<script type="text/javascript"> <!-- archiac attribute: language="JavaScript1.2">
// From: http://www.codingforums.com/showthread.php?t=236798

function showbelgium() { top.location.href = 'http://www.example.com/en/belgium.html'; }
function showluxembourg() { top.location.href = 'http://www.example.com/en/luxembourg.html'; }
function showmorocco() { top.location.href = 'http://www.example.com/en/morocco.html'; }
function shownetherlands() { top.location.href = 'http://www.example.com/en/thenetherlands.html'; }
function showsingapore() { top.location.href = 'http://www.example.com/en/singapore.html'; }
function showuk() { top.location.href = 'http://www.example.com/en/uk.html'; }
function showus() { top.location.href = ' http://www.example.com/en/usa.html'; }
function showrestofworld() { top.location.href = 'http://www.example.com/en' }

var language = geoip_country_code();
var code = language.substring(0,2);

switch (code) {
case 'BE': showbelgium(); break;
case 'LU': showluxembourg(); break;
case 'MA': showmorocco(); break;
case 'NL': shownetherlands(); break;
case 'SG': showsingapore(); break;
case 'GB': showuk(); break;
case 'US': showus(); break;
default: showrestofworld(); break;
}

</script>

desiter
09-02-2011, 06:37 PM
debi5767, at first please bring your SCRIPT tags to the present and replace language="JavaScript" by type="text/javascript" like jmrker suggested, so the begginning is more like:

<script type="text/javascript"src="http://j.maxmind.com/app/geoip.js"></script>

<script type="text/javascript"><!--

No a bit of lecture...

top.location.href - refers to the top (in the whole hierarchy) window location. If you use it inside an iframe or popup it will change the top parent's location which mean that the main browser window (or a tab) will be reloaded with a new document.
INPORTANT: if an iframes or a popup is loaded from other domain than their parent they have no access to their parent window so each attempt of changing the top's location (and any other manipulation) won't work.

parent.location.href - refers to the parent (on step up in the hierarchy) location. In case of nested iframes or popup opened by another popup will reload only the parent leaving the main window (tab) in piece.
INPORTANT: same restrictions as mentioned above.

location.href (same as window.location.href) - refers to current window and works perfectly without any restrictions. If you try it inside a popup or iframe then its content will be replaced (not the parents one).

To summarize... debi5767, do you use the code inside an iframe loaded from a different domain?

Ps. jmrker document.location.href is rather incorrect and works only thanks to some browsers' goodwill ;)

jmrker
09-02-2011, 09:07 PM
...
Ps. jmrker document.location.href is rather incorrect and works only thanks to some browsers' goodwill ;)

That was the un-tested part that I could not remember off the top of my head. :o

debi5767
09-05-2011, 08:44 AM
The following is untested code, but it does simplify you multiple if-else statements
(which I would always recommend putting between { } pairs
See: http://www.codingforums.com/showthread.php?t=236556 post #14).

I'm also not sure the "top.location.href=" is correct.
I have always used "document.location.href=" in the past,
but if it works for you then go for it!



<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>

<script type="text/javascript"> <!-- archiac attribute: language="JavaScript1.2">
// From: http://www.codingforums.com/showthread.php?t=236798

function showbelgium() { top.location.href = 'http://www.example.com/en/belgium.html'; }
function showluxembourg() { top.location.href = 'http://www.example.com/en/luxembourg.html'; }
function showmorocco() { top.location.href = 'http://www.example.com/en/morocco.html'; }
function shownetherlands() { top.location.href = 'http://www.example.com/en/thenetherlands.html'; }
function showsingapore() { top.location.href = 'http://www.example.com/en/singapore.html'; }
function showuk() { top.location.href = 'http://www.example.com/en/uk.html'; }
function showus() { top.location.href = ' http://www.example.com/en/usa.html'; }
function showrestofworld() { top.location.href = 'http://www.example.com/en' }

var language = geoip_country_code();
var code = language.substring(0,2);

switch (code) {
case 'BE': showbelgium(); break;
case 'LU': showluxembourg(); break;
case 'MA': showmorocco(); break;
case 'NL': shownetherlands(); break;
case 'SG': showsingapore(); break;
case 'GB': showuk(); break;
case 'US': showus(); break;
default: showrestofworld(); break;
}

</script>



thank you so much jmrker for your reply - let me try this !!

debi5767
09-05-2011, 08:47 AM
debi5767, at first please bring your SCRIPT tags to the present and replace language="JavaScript" by type="text/javascript" like jmrker suggested, so the begginning is more like:

<script type="text/javascript"src="http://j.maxmind.com/app/geoip.js"></script>

<script type="text/javascript"><!--

No a bit of lecture...

top.location.href - refers to the top (in the whole hierarchy) window location. If you use it inside an iframe or popup it will change the top parent's location which mean that the main browser window (or a tab) will be reloaded with a new document.
INPORTANT: if an iframes or a popup is loaded from other domain than their parent they have no access to their parent window so each attempt of changing the top's location (and any other manipulation) won't work.

parent.location.href - refers to the parent (on step up in the hierarchy) location. In case of nested iframes or popup opened by another popup will reload only the parent leaving the main window (tab) in piece.
INPORTANT: same restrictions as mentioned above.

location.href (same as window.location.href) - refers to current window and works perfectly without any restrictions. If you try it inside a popup or iframe then its content will be replaced (not the parents one).

To summarize... debi5767, do you use the code inside an iframe loaded from a different domain?

Ps. jmrker document.location.href is rather incorrect and works only thanks to some browsers' goodwill ;)

Thank desiter - this is very clear now - let me try this code, then i will let you know

debi5767
09-05-2011, 11:51 AM
Hi
Actually i am using joomla for my website - and geoip JavaScript file for redirection part of my website, as we have different country home page.

Let me give you some more information on my website structure: I have two file in my root folder
/default.html
/index.php

And the redirection code is inside default .html page – I want something like, when one user access my site he/she will 1st come to default.html page and thereafter geoip finds the location and then redirect them to specific page
Followed by index.php

Like if someone from UK access my site http://www.example.com he/she will redirect to
http://www.example.com/index.php?id=uk12

but i think jabasscript is not working again, after i made change as you give it to me - the page will refresh all the time - with title

http://www.example.com/index.php?id=uk12


i think geoip is able to find the location but javascript is unble to load the page

please help me

thank you...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum