[英]Not sure what this JS code is doing
I was given this JavaScript code snippet to implement on a site. 我获得了此JavaScript代码段以在网站上实现。 Just sticking it in is not working.
只是坚持不起作用。 I feel if i can better understand what this code is doing then i could make it work.
我觉得如果我能更好地理解这段代码在做什么,那么我就可以使它工作。 Could someone please explain roughly what this code is doing?
有人可以大致解释一下这段代码在做什么吗? Thanks!
谢谢!
<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>
The second and third lines are inserting a script
tag into the document, in order to load the script http://tracking.callmeasurement.com/clickx/click_matrix.cfm
. 第二行和第三行将
script
标记插入文档中,以便加载脚本http://tracking.callmeasurement.com/clickx/click_matrix.cfm
。 It's using http
if the protocol of the page is http
, https
if it's https
. 如果页面的协议为
http
,则使用http
如果为https
,则使用https
。 (You can ditch that part; more here .) It's passing the page's referrer as a parameter to the GET that loads the script (with &
replaced with zzzzz
), and also including a munique
parameter that just gets the numeric version of the current date, as an attempt to defeat caching. (您可以放弃该部分; 更多内容请参见 。)它将页面的引荐来源网址作为参数传递给加载脚本的GET(用
&
替换为zzzzz
),并且还包括一个仅获取当前日期数字版本的munique
参数。 ,以试图击败缓存。
The reason for the unescape
call and the encoded characters are that if you have </script>
inside a string in JavaScript code that's inside a script
tag, you'll prematurely end the script tag. unescape
调用和编码字符的原因是,如果您在script
标记内的JavaScript代码的字符串中包含</script>
,则会过早结束该脚本标记。 (One of several reasons JavaScript code should be in its own file.) So they're avoiding the HTML parser seeing </script>
by encoding the brackets, and using unescape
to update them. (JavaScript代码应放在其自己的文件中的几种原因之一。)因此,它们通过对方括号进行编码并使用
unescape
来更新,从而避免了HTML解析器看到</script>
。
Update : Below you said you're trying to use that code in window.load
. 更新 :您在下面说您正在尝试在
window.load
使用该代码。 You can't do that, because you can only use document.write
during the initial parse of the page. 您不能这样做,因为您只能在页面的初始解析期间使用
document.write
。 (If you use it later, it reopens the document — which clears it — and then writes to the new, blank document.) (如果以后使用它,它将重新打开文档(将其清除),然后写入新的空白文档。)
You can, though, add a script
tag later via another mechanism ( update : sorry, missed you were using jQuery; at the very bottom is a jQuery version): 不过,您可以稍后通过另一种机制添加
script
标签( update :对不起,很想念您正在使用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>
Or if you want to use the trick I linked to above about the 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>
And you don't really need a separate variable for the thisref
part; 而且,您对于
thisref
部分确实不需要单独的变量; changing that and stripping out the explanation above: 更改它并删除上面的解释:
<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>
And finally, I'd missed that you were using jQuery, sorry 'bout that: 最后,我想念您使用的是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>
...although you might consider jQuery(...
rather than $(window).load(...
unless you really want this to wait until all images and such are loaded (which maybe you do!). ...尽管您可能会考虑使用
jQuery(...
而不是$(window).load(...
除非您真的希望它等待所有图像等都被加载(也许您会这样做!)。
I actually prefer the non-jQuery version, even when using jQuery, but people's tastes are different... 我实际上更喜欢非jQuery版本,即使在使用jQuery时也是如此,但是人们的口味是不同的...
It is just trying to load a script from the server. 它只是试图从服务器加载脚本。 It is sending out a dummy referer information too.
它也正在发送伪引用信息。
First, it munges the URL of the referring page to replace ampersands with 'zzzzz'. 首先,它修改引荐页的网址,用“ zzzzz”代替“&”号。
Next, it figures out whether the protocol used to access the current page was HTTP or HTTPS. 接下来,它确定用于访问当前页面的协议是HTTP还是HTTPS。
Finally, it creates a script tag, with a src element set to a tracking site and feeds it the modified referer URL, with a unique ID based on the time, using the same protocol as was used to access the current page. 最后,它使用与访问当前页面相同的协议,创建一个脚本标签,并在跟踪站点中设置一个src元素,并向其提供经过修改的引荐来源网址,并具有基于时间的唯一ID。
In short, it's tracking code to determine where arrivers at this page came from. 简而言之,它是跟踪代码,以确定此页面的到达者来自何处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.