SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript external remote file question

    Hello

    I am having trouble with a simple JavaScript issue, maybe you guys have a suggestion, thanks in advance.

    Ok, i have my empty index.html that i want to redirect visitors with, to another page on my site root with the window.location method, for example if the ip address comes from Germany then i use the window.location to point to "main.php?lang=de", if the country is any other, i point to the english version "main.php?lang=en".

    So i am attaching to the head tag this useful script from GeoIP, that links directly to their site "j.maxmind.com/app/geoip.js".

    Then i attach my own .js file with my code.
    In this file i do my simple GeoIP processing like this :

    Code JavaScript:
       //  I set my variable equal to the function from the geoip.js file that 
       //  returns as string, the country code of the visitor.
       var userCountry = geoip_country_code();
     
      // Then i check the country code and redirect to the coresponding language
       if (userCountry == "DE") {  
          window.location = "main.php?lang=de"
       }
       else { 
          window.location = "main.php?lang=en" 
       }

    So far so good. But i want to specify a 3rd option, maybe an "else if" in the middle of my condition in my .js file, that redirect visitors also to the english page "main.php?lang=en", but this time, when the geoip.js file fails to load (for when the website thats hosts geopip.js is down, or maybe file is moved, or whatever).

    Here i got stuck for a whole day, my javascript isn't so good :-s.

    So i am asking, is there a way to find out if an external remote .js file loaded successfully or not?

    And one more curiousity, if it fails to load, is the parsing stopped or broken?

    Thanks alot. See you around.
    Last edited by poznicu; Nov 16, 2008 at 21:37. Reason: typos

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Hi

    Maybe you could wrap the if/else (and the var assignment) with
    HTML Code:
    if(geoip_country_code){
    or
    HTML Code:
    if(typeof geoip_country_code == 'function'){
    or maybe a try/catch on the initial assignment something like
    HTML Code:
    try{
      var userCountry = geoip_country_code();
    }
    catch(e)
    {
    window.location = "main.php?lang=en";
    }
    Not tested, but it gives you something to try.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Use this:

    Code javascript:
    var userCountry = geoip_country_code() || 'EN';

    So that if the geoip_country_code function doesn't return a useful value, a default of 'EN' will be applied instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply guys, i will give them a try.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Thanks for posting that pmw57, a much cleaner way to do it

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You're welcome - it was inspired from event experiences. Such as:

    Code javascript:
    function(evt) {
        evt = evt || window.event;
        var targ = evt.target || evt.srcElement;
        ...
    }

    When you're not expecting falsy values such as 0 or false, it can be an effective way to provide default or optional values.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Use this:

    Code javascript:
    var userCountry = geoip_country_code() || 'EN';

    So that if the geoip_country_code function doesn't return a useful value, a default of 'EN' will be applied instead.
    This didn't work, when i changed the script name to something bogus so the index.html could not possibly load it, the page just came blank and did nothing.

    However, the try and catch method worked, here is the full code of the pages :

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h.t.t.p://w.w.w.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="h.t.t.p://w.w.w.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="h.t.t.p://j.maxmind.com/app/geoip.js"></script>
    <script type="text/javascript" src="index.js"></script>
    </head>
    <body>
    <noscript>
    <p>EN : This website requires 'javascript' enabled to run.</p>
    </noscript>
    </body>
    </html>

    This is index.js :

    Code JavaScript:
       try{
       var userCountry = geoip_country_code();
       }
       catch(e)
          {
       var userCountry = "OTHER";
       }  
     
       if (userCountry == "DE") { 
          window.location = "main.php?lang=de"
       }
       else {
          window.location = "main.php?lang=en"
       }

    Hope this helpes other users, thanks for the feedback.
    Last edited by poznicu; Nov 17, 2008 at 18:14. Reason: typo

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Shucks, that's disappointing, not that you got it sorted, that's good, I mean the || technique.
    I wonder if the call needs to be in parentheses to work?
    HTML Code:
    var userCountry = (geoip_country_code()) || 'EN';
    EDIT:
    Nope. I tried this
    HTML Code:
    <html><head><title>or test</title>
    <script type='text/javascript'>
    var isThere = function(){
    return 'a value';
    //return false;
    };
    
    var really = isThere() || 'isThere is false';
    //var bogus = notThere() || 'notThere is not there';
    
    alert(really);
    //alert(bogus);
    </script>
    </head><body></body></html>
    'really' was either 'a value' or 'isThere is false' depending on what isThere() returned.
    But notThere() threw an "object expected" error and did not assign a value to 'bogus'.
    Last edited by Mittineague; Nov 17, 2008 at 20:11.

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Ahh, well if we're dealing with functions that may not actually exist, then a different approach needs to be taken. You should define a stub function before the actual function is created.

    Code html4strict:
    <script>
    function geoip_country_code() {
        // stub function, in case the remote one doesn't load.
        return 'EN';
    }
    </script>
    <script src="remote-may-not-load.js"> </script>
    <script>
    var userCountry = geoip_country_code();
    </script>

    Now the local stub version of geoip_country_code() will be used is the remote code doesn't exist. If it does exist then it'll redefine the stub function to what it should be and things will progress as per normal.

    Note: The inline code is just to help demonstrate the flow of code. The local javascript code should be kept in external script files.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

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