Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript redirection code not working

    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


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

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    The following is untested code, but it does simplify you multiple if-else statements
    (which I would always recommend putting between { } pairs
    See: switch vs. if else 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!

    Code:
    <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>
    Last edited by jmrker; 09-02-2011 at 04:57 PM.

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Location
    Poland
    Posts
    8
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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:
    Code:
    <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
    Last edited by desiter; 09-02-2011 at 05:43 PM.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Arrow

    Quote Originally Posted by desiter View Post
    ...
    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.

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    The following is untested code, but it does simplify you multiple if-else statements
    (which I would always recommend putting between { } pairs
    See: switch vs. if else 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!

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

  • #6
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by desiter View Post
    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:
    Code:
    <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

  • #7
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    Last edited by debi5767; 09-06-2011 at 09:56 AM. Reason: thank you...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •