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?
<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&gateid=MTk2ODUx";
}
head.appendChild(script)
</script>
Thanks. Nick.
You missed the closing brace to the if(cc.toLowerCase() == "US".toLowerCase() || cc.toLowerCase() == "GB".toLowerCase())
statement.
Here is the revised code, errors fixed:
<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&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.
Hi. Thanks for the quick reply. However I still can’t get it to work. Can you see any other errors?
<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&gateid=MTk2ODUx";
}
head.appendChild(script)
</script>
Thanks again!
Nick.
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:
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.