Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅当GET变量不';不存在于URL中_Javascript_Php_Jquery - Fatal编程技术网

Javascript 仅当GET变量不';不存在于URL中

Javascript 仅当GET变量不';不存在于URL中,javascript,php,jquery,Javascript,Php,Jquery,你好,斯塔克弗斯 对于特定的需求,我想实现一个网站,如果URL中定义了特定的变量,我想排除对Google Analytics脚本的调用 我在网上搜索寻找一个简单的解决方案,但我读到的所有解决方案都显得太复杂了,涉及到用Javascript手动创建函数……等等 到目前为止,我发现的最简单(但有点脏)的解决方案涉及调用PHP代码段,如下所示: <?php if ($_GET['dnt']===null) { ?> <!

你好,斯塔克弗斯

对于特定的需求,我想实现一个网站,如果URL中定义了特定的变量,我想排除对Google Analytics脚本的调用

我在网上搜索寻找一个简单的解决方案,但我读到的所有解决方案都显得太复杂了,涉及到用Javascript手动创建函数……等等

到目前为止,我发现的最简单(但有点脏)的解决方案涉及调用PHP代码段,如下所示:

<?php
        if ($_GET['dnt']===null)
        {
        ?>
        <!-- Recaptcha Script -->
        <script src='https://www.google.com/recaptcha/api.js'></script> 

        <!-- Global site tag (gtag.js) - Google Analytics -->
        <script async src="https://www.googletagmanager.com/gtag/js?id=my_id"></script>
        <script>
        console.log('test');
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());

          gtag('config', 'my_id');
        </script>
        <?php } ?>
…但结果完全一样

你有什么简单的替代方法吗? 我的上述代码来源于本文:


谢谢。

自从我发布那条消息以来,我一直在调查这个问题,尽管测试了很多东西,我还是无法让PHP正确读取我的$\u get变量。我无法确定原因,因为没有人能够提供帮助,下面是我如何在jQuery中解决这个问题的:

var $_GET = {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));
    }

    $_GET[decode(arguments[1])] = decode(arguments[2]);
});

//If variable "dnt" is defined, bypass Google Analytics
if(typeof $_GET["dnt"] == 'undefined')
{
    var gA = "<script src='https://www.google.com/recaptcha/api.js'><\/script><script async src='https://www.googletagmanager.com/gtag/js?id=my_id'><\/script><script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'my_id');<\/script>";
    $('body').append(gA);
}
var$\u GET={};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g,函数(){
功能解码{
返回组件(s.split(“+”).join(“”);
}
$_GET[decode(参数[1])]=decode(参数[2]);
});
//如果定义了变量“dnt”,请绕过Google Analytics
if(typeof$\u GET[“dnt”]=“未定义”)
{
var gA=“window.dataLayer=window.dataLayer | |[];function gtag(){dataLayer.push(arguments)}gtag('js',new Date());gtag('config','my_id');”;
$('body')。追加(gA);
}

它很难看,但做得很好。

您使用的URL是什么?你确定你正确设置了
dnt
?我是这样使用的:localhost?dnt=1我的整个网站只有一页,索引页。:)您可以使用“if(!isset($\u GET['dnt'])”来回显javascript Google脚本标记。通过这种方式,您可以在未定义变量的情况下加载脚本标记。
var\u dump($\u get)
显示了什么?@CharlesEF我尝试了您的解决方案,结果完全相同。。。代码总是被调用的。
var $_GET = {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));
    }

    $_GET[decode(arguments[1])] = decode(arguments[2]);
});

//If variable "dnt" is defined, bypass Google Analytics
if(typeof $_GET["dnt"] == 'undefined')
{
    var gA = "<script src='https://www.google.com/recaptcha/api.js'><\/script><script async src='https://www.googletagmanager.com/gtag/js?id=my_id'><\/script><script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'my_id');<\/script>";
    $('body').append(gA);
}