Web Analytics Made Easy -
StatCounter JavaScript redirection code not working - CodingForum

Announcement

Collapse
No announcement yet.

JavaScript redirection code not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.codingforum.net/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!

    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.codingforum.net/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; Sep 2, 2011, 12:57 PM.

    Comment


    • #3
      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; Sep 2, 2011, 01:43 PM.
      Descript.org - lightweight JavaScript foundation framework

      Comment


      • #4
        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.

        Comment


        • #5
          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: http://www.codingforum.net/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!

          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.codingforum.net/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 !!

          Comment


          • #6
            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

            Comment


            • #7
              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; Sep 6, 2011, 05:56 AM. Reason: thank you...

              Comment

              Working...
              X