Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

javascript - Cannot get Geolocation script to work

I scripted the following code, but for some reason it does not work please help thanks

<script>
    $.getScript('http://www.geoplugin.net/javascript.gp', function() {

    $location =  geoplugin_countryCode();
$location2 = geoplugin_continentCode();        



    if($location == "CA" || $location == "US" || $location2 == "EU" || $location2 == "DE" || $location2 == "FR" || $location2 == "GB") {
    alert($location);
         var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9240923-27']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

    } else {

         var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9240923-27']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
    } 

});



        </script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is not a proper answer yet, as it is still unclear what the actual problem is, but to explain to OP, I'll post this as an answer.

$.getScript('http://www.geoplugin.net/javascript.gp', function() {

    $location =  geoplugin_countryCode();
    $location2 = geoplugin_continentCode();        

    if($location == "CA" || $location == "US" || $location2 == "EU" || $location2 == "DE" || $location2 == "FR" || $location2 == "GB") {
       alert($location);
    }
});

/* 
 * I'm not sure what you want to accomplish 
 * with loading Google Analytics within the success 
 * callback of getScript, so I moved it out here until that is clarified.
 */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9240923-27']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...