Hey all. This has been giving me some trouble lately. I'll post the code now and then discuss: <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> Code (markup): From what I can see in Chrome's Javascript console, the script is been appended correctly (for me that is the second script - Australia). However the script doesn't run because of errors. But, if I just include the script directly it works? - (I think there still might be some warnings or something, but it works like it should). What is the difference between appending and putting it there directly. Also is this the way I should be going about this? Thanks heaps! Nick.
What are the error(s) you're getting? You should be able to dynamically load JS like this fine though.
Here is the error and associated code: ERROR: Uncaught TypeError: Cannot read property 'style' of null var eukmcenbwxlyijob088b4ff0eee42857d9 = document.getElementById('elucdaaqcinwhyvtfd97126be431a73a42d9e26e82d4c4b5'); eukmcenbwxlyijob088b4ff0eee42857d9.style.display = 'block'; locker.php:265Uncaught TypeError: Cannot read property 'style' of null Code (markup): Now, if I just include the script directly I get a couple warnings but it works fine: <script src='http://trackpath.biz/locker.php?pub=31341&gateid=MTk2ODUx'></script> Code (markup): I just can't figure out what is going on here. Thanks again. Nick.
I assume their code (not yours) tries to create some elements on load of the page. But since the page has already loaded, maybe it doesn't. Either way, it's due to their code. It'd probably be easier to use PHP for this, if you're already using PHP elsewhere in the file. You can use geoIP's PHP library to find if they're from the UK or US, then use an if statement to determine which JS to include.
Thanks James. That makes sense. I'm implementing this on Blogspot - so no PHP for me. Is there a way I can do this is JS? Thanks again for all you help! You've been excellent. Nick.