English 中文(简体)
忽略单击时的忽略,以防止 JS 出错
原标题:Ignore an onclick to prevent a JS error

我使用古老的谷歌分析代码, 并有一堆链接, 上面附有页面跟踪功能, 以便每个点击都计算。 既然我升级到最新的 GA, 页面跟踪器不再有效, 这样可以, 但是链接显示 JS 错误, 因为页面跟踪器没有定义 。 我不想通过所有链接, 在单击时删除所有的页面跟踪器, 但是只要找到一个方法来取消这些链接, 这样它们不会造成错误 。

我尝试了 var pageTracker = ""; 和 var pageTracker = null; 但仍然有一个未定义的错误。 这是链接看起来像的 。

<p><a href="/gotowebsite/" onclick="javascript: pageTracker._trackPageview( /GoToTheWebsiteLink );">Go to the website</a><br />

Here it is in JSFiddle. When you click the link in IE, you see the JS error for a brief period of time till the next page loads. http://jsfiddle.net/Fvp9F/

问题回答

你非常亲近,只需要把它变成一个空白的物体:

var pageTracker = {_trackPageview: function(){}};

试着添加一个假的 pageTracker 对象(当然,它最好在未事先定义的情况下才能这样做) 。

if (!pageTracker) { /* if pageTracker is not defined */
    var pageTracker = {
        "_trackPageview" : function() {

        }            
    }
}

例如:"http://jsfiddle.net/Fvp9F/6" rel="nofollow">http://jsfiddle.net/Fvp9F/6/

比添加假的/空白的页面跟踪器对象要好得多, 你应该能够从 _gat 全球获得页面跟踪器, 并且仍然有旧的跟踪代码工作 。

pageTracker = _gat._getTrackerByName();

一个问题是,您需要确保追踪器在 < / em > 之后获得自动同步跟踪代码已装入。 也允许函数对象 -- -- 在您的自动同步跟踪代码中添加以下内容应该有效 :

_gaq.push(function() { pageTracker = _gat._getTracker(); });




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签