不确定这个JS代码在做什么

时间:2011-02-22 17:21:34

标签: 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>

3 个答案:

答案 0 :(得分:8)

第二行和第三行正在向文档中插入script标记,以便加载脚本http://tracking.callmeasurement.com/clickx/click_matrix.cfm。如果页面的协议为http,则http使用https,如果是https,则使用&。 (你可以放弃那部分; more here。)它将页面的引用者作为参数传递给加载脚本的GET(zzzzz替换为munique),还包括{ {1}}参数,它只获取当前日期的数字版本,以试图阻止缓存。

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

<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,但人们的口味也不同......

答案 1 :(得分:2)

它只是尝试从服务器加载脚本。它也发送了一个虚拟参考信息。

答案 2 :(得分:1)

首先,它会使用引用页面的URL来替换带有'zzzzz'的&符号。

接下来,它确定用于访问当前页面的协议是HTTP还是HTTPS。

最后,它创建了一个脚本标记,其中src元素设置为跟踪站点,并使用与访问当前页面时相同的协议,使用基于时间的唯一ID为其提供修改后的引用URL。

简而言之,它是跟踪代码以确定此页面的到达者来自哪里。

相关问题