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

Rya*_*tts 0 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>
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 8

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

unescape调用和编码字符的原因是,如果您</script>在JavaScript代码中包含一个位于script标记内的字符串,则会过早地结束脚本标记.(JavaScript代码应该在其自己的文件中的几个原因之一.)因此,他们</script>通过编码括号并使用unescape更新它们来避免HTML解析器的查看.


更新:下面你说你正在尝试使用该代码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>
Run Code Online (Sandbox Code Playgroud)

或者如果你想使用我上面链接的关于协议的技巧:

<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>
Run Code Online (Sandbox Code Playgroud)

而且你并不需要为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>
Run Code Online (Sandbox Code Playgroud)

最后,我错过了你使用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>
Run Code Online (Sandbox Code Playgroud)

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

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