Not sure what this JS code is doing
I was given this JavaScript code snippet to implement on a site. Just sticking it in is not working. I feel if i can better und开发者_StackOverflow中文版erstand 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
. It's using http
if the protocol of the page is http
, https
if it's 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.
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. (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.
Update: Below you said you're trying to use that code in window.load
. You can't do that, because you can only use document.write
during the initial parse of the page. (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 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; 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:
<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!).
I actually prefer the non-jQuery version, even when using jQuery, but people's tastes are different...
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'.
Next, it figures out whether the protocol used to access the current page was HTTP or 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.
In short, it's tracking code to determine where arrivers at this page came from.
精彩评论