帮助重构一小段Javascript代码,以识别用户的引荐来源 [英] Help refactor a small piece of Javascript code which identifies user's referrer source
本文介绍了帮助重构一小段Javascript代码,以识别用户的引荐来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了以下一小段javascript(基于出色的 parseURI 函数)以识别用户的来源。我是Java语言的新手,尽管下面的代码有效,但是想知道是否有更有效的方法来实现相同的结果?
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 code below works, was wondering if there is a more efficient method of achieving this same result?
try {
var path = parseUri(window.location).path;
var host = parseUri(document.referrer).host;
if (host == '') {
alert('no referrer');
}
else if (host.search(/google/) != -1 || host.search(/bing/) != -1 || host.search(/yahoo/) != -1) {
alert('Search Engine');
}
else {
alert('other');
}
}
catch(err) {}
推荐答案
您可以使用其他搜索来简化主机检查:
You can simplify the host check using alternative searches:
else if (host.search(/google|bing|yahoo/) != -1 {
我也会在提取主机中的无推荐人错误之前,请先尝试测试文档推荐人。
I'd also be tempted to test document referrer before extracting the host for your "no referrer" error.
(我尚未测试过)。
这篇关于帮助重构一小段Javascript代码,以识别用户的引荐来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文