Help Refactor A Small Piece Of Javascript Code Which Identifies User's Referrer Source
I've written the following small piece of javascript (Based on the excellent parseURI function) to identify where the user originated from. I am new to Javascript, and although the
Solution 1:
You can simplify the host check using alternative searches:
elseif (host.search(/google|bing|yahoo/) != -1 {
I'd also be tempted to test document referrer before extracting the host for your "no referrer" error.
(I've not tested this).
Solution 2:
I end up defining a function called set
in a lot of my projects. It looks like this:
functionset() {
var result = {};
for (var i = 0; i < arguments.length; i++)
result[arguments[i]] = true;
return result;
}
Once you've got the portion of the hostname that you're looking for...
// low-fi way to grab the domain name without a regex; this assumes that the// value before the final "." is the name that you want, so this doesn't work// with .co.uk domains, for examplevar domain = parseUri(document.referrer).host.split(".").slice(-2, 1)[0];
...you can elegantly test your result against a list using JavaScript's in
operator and the set
function we defined above:
if (domain inset("google", "bing", "yahoo"))
// do stuff
More info:
Post a Comment for "Help Refactor A Small Piece Of Javascript Code Which Identifies User's Referrer Source"