【问题标题】:Cannot get Geolocation script to work无法使地理定位脚本工作
【发布时间】:2012-07-12 08:27:36
【问题描述】:

我编写了以下代码,但由于某种原因它不起作用,请帮忙谢谢

<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>

【问题讨论】:

  • 你的意思是它不起作用?会发生什么,您期望会发生什么?
  • 它没有显示在谷歌分析中
  • 我不确定我是否了解您想在这里完成什么。您在 if 和 else 子句中都加载分析?为什么不在 if 语句之后做呢?为什么在getScript 的成功回调中加载 Google Analytics?
  • 1 分析代码将被修改 2. 请您解释一下:最好举个例子
  • 我发布了您的问题的答案,概述了我编写此代码的方式。由于我不知道您想通过在成功回调中加载 Google Analytics 来完成什么,因此您必须多解释一下您想要完成什么。

标签: javascript geolocation google-analytics


【解决方案1】:

这还不是一个正确的答案,因为目前还不清楚实际问题是什么,但为了向 OP 解释,我会将此作为答案发布。

$.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);
})();

【讨论】:

    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    相关资源
    最近更新 更多