SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Choosing which script to to run? Noob Q.

    Hey all. I'm trying to choose which script I want to run based on the user's country, but I'm not having much luck. My script is below, can you please let me know what I'm doing wrong?

    Code:
    <script language='JavaScript' src='http://www.geoplugin.net/javascript.gp' type='text/javascript'/>
    
    <script language='Javascript'>
      var head = document.getElementsByTagName('head')[0];
      script = document.createElement('script');
      script.type = 'text/javascript';
      var cc = geoplugin_countryCode();
      if(cc.toLowerCase() == "US".toLowerCase() || cc.toLowerCase() == "GB".toLowerCase()){
        script.src = "http://interstitial.powered-by.secure-  softwaremanager.com/fd2fed27b5ce840faea85789afd78fb135da";
      else{
        script.src="http://trackpath.biz/locker.php?pub=31341&amp;gateid=MTk2ODUx";
      }
      head.appendChild(script)
    </script>
    Thanks. Nick.
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos

  2. #2
    SitePoint Guru bronze trophy TheRaptor's Avatar
    Join Date
    Jul 2011
    Location
    New York
    Posts
    710
    Mentioned
    40 Post(s)
    Tagged
    0 Thread(s)
    You missed the closing brace to the if(cc.toLowerCase() == "US".toLowerCase() || cc.toLowerCase() == "GB".toLowerCase()) statement.

    Here is the revised code, errors fixed:
    Code JavaScript:
    <script src="http://www.geoplugin.net/javascript.gp"></script>
     
    <script>
      var head = document.getElementsByTagName('head')[0];
      script = document.createElement('script');
      script.type = 'text/javascript';
      var cc = geoplugin_countryCode();
      if(cc.toLowerCase() == "US".toLowerCase() || cc.toLowerCase() == "GB".toLowerCase()){
        script.src = "http://interstitial.powered-by.secure-  softwaremanager.com/fd2fed27b5ce840faea85789afd78fb135da";
      } else {
        script.src="http://trackpath.biz/locker.php?pub=31341&amp;gateid=MTk2ODUx";
      }
      head.appendChild(script)
    </script>

    By the way, language ="JavaScript" is an old outdated way of telling the browser the script your calling is JS, we use type="text/javascript" now. And with the event of HTML5, we can drop that too. Also the <script> tag can't be self closed.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi. Thanks for the quick reply. However I still can't get it to work. Can you see any other errors?

    Code JavaScript:
    <script src='http://www.geoplugin.net/javascript.gp'></script> 
    <script> 
      var head = document.getElementsByTagName('head')[0];
      script = document.createElement('script');
      script.type = 'text/javascript';
      var cc = geoplugin_countryCode();
      if(cc.toLowerCase() == "US".toLowerCase() || cc.toLowerCase() == "GB".toLowerCase()){
        script.src = "http://interstitial.powered-by.secure-softwaremanager.com/fd2fed27b5ce840faea85789afd78fb135da";
      } else {
        script.src = "http://trackpath.biz/locker.php?pub=31341&amp;gateid=MTk2ODUx";
      }
      head.appendChild(script)
    </script>

    Thanks again!

    Nick.
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, after taking a deeper look I've found that the script is being appended properly. However, the script it throwing errors which it doesn't if I just include it directly.

    Chrome javascript console is telling me this:

    Code JavaScript:
      var szqmkgnskgdkduju25834fa5533c80a99192961c6a85 = document.getElementById('udyfvojccgzjjdyod3275310571cd4');
        szqmkgnskgdkduju25834fa5533c80a99192961c6a85.style.display = 'block';
     
    Uncaught TypeError: Cannot read property 'style' of null

    However when I include the second script directly (see code in previous post) it works. So what is changing? I have the code in the head of the document.

    Also, before trying this append script, I was using document.write(), and I think that might have been working but I'm not sure. Anyway, just giving you as much information as I have.

    Thanks.

    Nick.
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos


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
  •