javascript - 不确定这段 JS 代码在做什么

标签 javascript

我得到了这个 JavaScript 代码片段,需要在网站上实现。光是插进去是不行的。我觉得如果我能更好地理解这段代码的作用,那么我就能让它工作。有人可以大致解释一下这段代码在做什么吗?谢谢!

<script type="text/javascript">
var thisref = document.referrer.replace(/&/g, "zzzzz");
var ciJsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write(unescape("%3Cscript src='" + ciJsHost + "tracking.callmeasurement.com/clickx/click_matrix.cfm?munique=" + new Date().getTime() + "&prev=" + thisref + "' type='text/javascript'%3E%3C/script%3E"));
</script>

最佳答案

第二行和第三行插入 script标记到文档中,以便加载脚本 http://tracking.callmeasurement.com/clickx/click_matrix.cfm 。它正在使用 http如果页面的协议(protocol)是 http , https如果是https 。 (您可以放弃该部分; more here 。)它将页面的引用作为参数传递给加载脚本的 GET (将 & 替换为 zzzzz ),并且还包括 munique参数仅获取当前日期的数字版本,以尝试击败缓存。

unescape的原因调用和编码字符是,如果你有 </script> JavaScript 代码中的字符串位于 script 内标记,您将提前结束脚本标记。 (JavaScript 代码应该位于自己的文件中的几个原因之一。)因此,他们避免 HTML 解析器看到 </script>通过对括号进行编码,并使用 unescape更新它们。

<小时/>

更新:下面您说您正在尝试在 window.load 中使用该代码。你不能这样做,因为你只能使用 document.write在页面的初始解析期间。 (如果您稍后使用它,它会重新打开文档 - 从而清除它 - 然后写入新的空白文档。)

不过,您可以添加 script稍后通过另一种机制标记(更新:抱歉,错过了您使用 jQuery;最底部是 jQuery 版本):

<script type="text/javascript">
window.onload = function() {
    // Get the referrer, replacing `&` with `zzzzz`
    var thisref = document.referrer.replace(/&/g, "zzzzz");

    // Get the protocol to use based on the current document's protocol
    var ciJsHost = (("https:" == document.location.protocol) ? "https://" : "http://");

    // Create a script element, set its type and src
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = ciJsHost
                 + "tracking.callmeasurement.com/clickx/click_matrix.cfm?munique="
                 + new Date().getTime()
                 + "&prev=" + thisref;

    // Add it just about anywhere; `document.body` works fine and is
    // easy. The script is downloaded and executed when you do the append.
    document.body.appendChild(script);
};
</script>

或者如果你想使用这个技巧 I linked to above关于协议(protocol):

<script type="text/javascript">
window.onload = function() {
    // Get the referrer, replacing `&` with `zzzzz`
    var thisref = document.referrer.replace(/&/g, "zzzzz");

    // Create a script element, set its type and src
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = "//tracking.callmeasurement.com/clickx/click_matrix.cfm?munique="
                 + new Date().getTime()
                 + "&prev=" + thisref;

    // Add it just about anywhere; `document.body` works fine and is
    // easy. The script is downloaded and executed when you do the append.
    document.body.appendChild(script);
};
</script>

并且您实际上并不需要 thisref 的单独变量部分;更改它并删除上面的解释:

<script type="text/javascript">
window.onload = function() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = "//tracking.callmeasurement.com/clickx/click_matrix.cfm?munique="
                 + new Date().getTime()
                 + "&prev=" + document.referrer.replace(/&/g, "zzzzz");
    document.body.appendChild(script);
};
</script>

最后,我错过了您正在使用 jQuery,对此表示抱歉:

<script type="text/javascript">
$(window).load(function() {
    $("<scr" + "ipt type='text/javascript' src='//tracking.callmeasurement.com/clickx/click_matrix.cfm?munique="
        + new Date().getTime()
        + "&prev=" + document.referrer.replace(/&/g, "zzzzz") + "'></src" + "ipt>")
        .appendTo(document.body);
});
</script>

...尽管您可能会考虑 jQuery(...而不是$(window).load(...除非你真的希望等到所有图像等都加载完毕(也许你会这样做!)。

我实际上更喜欢非 jQuery 版本,即使使用 jQuery 时也是如此,但人们的口味不同......

关于javascript - 不确定这段 JS 代码在做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5081529/

相关文章:

Javascript Button - onClick 之后不会调用方法

javascript - 当凭据不匹配时,阻止登录表单提交(AJAX)(返回 false 不起作用)

java - 编码对 JSON 重要吗?

javascript - 在 sails js 应用程序中使用 "require"

JavaScript Canvas 不工作

javascript - 如何在页面加载后 10 秒显示弹出窗口?

javascript - 使用多个可选择的选项卡

javascript - 如何同时使用AngularJS作为前端框架和ASP.NET MVC作为后端框架?

javascript - 预加载 jPlayer 使用的音频文件

JavaScript : Split string using function and returning it