我正在处理这个 javascript 代码,当它进入头部时,我刷新它进入 404 页面。
请有人帮忙。
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
try {
var myTracker = _gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category, action]);
setTimeout('document.location = "' + link.href + '"', 100)
} catch (err) { }
}
$(document).ready(function () { $('#myid').click(recordOutboundLink(this, 'regular xxxxx', 'xxxx.example.com')); });
</script>
最佳答案
您正在尝试将 recordOutboundLink()
的结果注册为点击处理程序,导致该函数首先运行,评估 window.href
作为要重定向的页面到。 window.href
的值通常为 undefined
,因此浏览器将尝试重定向到 http://undefined
或类似内容。
相反,您应该只在单击某些内容时执行该函数,如下所示:
$(document).ready(function () {
$('#myid').click(function() {
recordOutboundLink(this, 'regular xxxxx', 'http://xxxx.example.com');
return false;
});
我相信 Google 文档提到了这样的事情:
<a href="bla bla" onclick="recordOutboundLink(this, 'regular crap', 'http://www.example.com'); return false;">tada click me</a>
编辑
您的位置应该始终是绝对的,即以 http://
、https://
或简单的 //
开头。
关于javascript代码使url链接未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13683878/