www.webdeveloper.com
Results 1 to 3 of 3

Thread: Calling Google JSapi

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Question Calling Google JSapi

    Apologies in advance if this is a stupid question, it seems incredibly simple, I'm both new here and to JavaScript in general.

    I'm trying to modify a Wordpress plug-in which fails if the Google JSapi doesn't find a location from the visitor's IP address. I figured that the if statement show below should solve the problem (I'm trying to make it select GB as the country code if the GoogleApi doesn't return US or GB)

    function checkAmazonLinks() {
    // set required VARIABLES
    var strTld;
    var objRegexAsin = new RegExp( '\/([A-Z0-9]{10})' );
    var strCountry = google.loader.ClientLocation.address.country_code;

    if (strCountry == 'US' || strCountry == 'GB'){
    }
    else {
    strCountry = 'GB'
    }
    ...

    What's weird is that it works fine if I set strCountry to a random string it works correctly.

    I'm testing the code at www.gizunk.co.uk/test but I'd imagine it won't be a lot of help, for the vast majority of you it won't help as the Google JSapi will work correctly. For some reason my ISP (Be broadband UK) seems to have a problem with it.

    Thanks for any help, I'm sure I'm just being a JavaScript noob!

    Ben

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    You have a call to google-
    <script src="http://www.google.com/jsapi"></script>

    What you may need is to define the global variable strCountry before making that call.
    Move your script ahead of the call

  3. #3
    Join Date
    Jun 2010
    Posts
    2
    Hey thanks for getting to back to me. I had actually called Google with the script in a different place, sorry I should have put that in my code.

    I've since discovered what was going wrong, gsapi seems to fail on certain ISP's Ip addresses giving a return of null for google.loader.ClientLocation which makes the code crash. The way I got round that was to use maxmind as an alternate geolocator as shown below.

    function checkAmazonLinks() {
    // set required VARIABLES
    var strTld;
    var objRegexAsin = new RegExp( '\/([A-Z0-9]{10})' );

    // Incase google isn't working for IP.
    if ( google.loader.ClientLocation ) {
    var strCountry = google.loader.ClientLocation.address.country_code;
    } else {
    //If Google JSapi fails try
    strCountry = geoip_country_code();
    }
    etc...

    As with Google you also need to call it - <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>

    Thanks for the help anyway, I hope the above helps anyone else with the same problems as me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles