admin管理员组文章数量:1302404
I am using ASP.NET to create a small web app. One of my pages includes some LinkButton controls, which used to work fine until I added a reference to Google Analytics code to it. Now, when I click on a link button, I get the error message:
Microsoft JScript runtime error: The value of the property '__doPostBack' is null or undefined, not a Function or object
The other links and controls on the page work fine. If I remove the reference to the Google Analytics script from the page, everything works fine as well. It seems that the problem arises from the interaction between the Google Analytics script and the LinkButton controls trying to postback the page.
UPDATE. I have further observed the following. When no Google Analytics script reference is present, the HTML that ASP.NET generates looks fine:
However, as soon as I add Google Analytics code, the HTML gets screwed:
Check out that form tag! I imagine now that the postback error arises from the fact that the linkbutton controls get placed outside the ASP.NET form. But why? END UPDATE.
Any ideas on how to solve this? Thanks.
FURTHER UPDATE. After much experimenting, I have been able to solve this by myself. I have added an answer below showing my conclusions. Thanks to all who have posted answers here. END UPDATE.
I am using ASP.NET to create a small web app. One of my pages includes some LinkButton controls, which used to work fine until I added a reference to Google Analytics code to it. Now, when I click on a link button, I get the error message:
Microsoft JScript runtime error: The value of the property '__doPostBack' is null or undefined, not a Function or object
The other links and controls on the page work fine. If I remove the reference to the Google Analytics script from the page, everything works fine as well. It seems that the problem arises from the interaction between the Google Analytics script and the LinkButton controls trying to postback the page.
UPDATE. I have further observed the following. When no Google Analytics script reference is present, the HTML that ASP.NET generates looks fine:
However, as soon as I add Google Analytics code, the HTML gets screwed:
Check out that form tag! I imagine now that the postback error arises from the fact that the linkbutton controls get placed outside the ASP.NET form. But why? END UPDATE.
Any ideas on how to solve this? Thanks.
FURTHER UPDATE. After much experimenting, I have been able to solve this by myself. I have added an answer below showing my conclusions. Thanks to all who have posted answers here. END UPDATE.
Share edited Jun 6, 2011 at 11:52 CesarGon asked Jun 3, 2011 at 14:28 CesarGonCesarGon 15.3k8 gold badges59 silver badges87 bronze badges 7- Can we see the relevant sections of the source for your page? Check that your script tags are closed and perhaps try moving them to the end of the page rather than the beginning. – mikey Commented Jun 5, 2011 at 15:59
- @mikey: All the tags are properly closed; I have quadruple (at least) checked. I will post some source tomorrow as soon as I get to my office. What sections of the page source would you like to see? I will also try moving the reference to the Google Analytics script to the end of the page. Thanks. – CesarGon Commented Jun 5, 2011 at 17:24
- Please check that you're closing the <head> tag also. I've never seen such strangeness, I do think it is something small and easy to miss like that though. Beyond that I'd probably start looking at the .js code itself for anything odd. – mikey Commented Jun 5, 2011 at 17:52
-
Just to clarify, are you pasting the Google Analytics code right at the end of your page (Just before
</body>
)? – keyboardP Commented Jun 5, 2011 at 17:58 - @keyboardP: No. I am pasting it right before the closing </head> tag, as Google suggests. – CesarGon Commented Jun 6, 2011 at 11:09
5 Answers
Reset to default 3After lots of tweaking, tech support and experimenting, I have fixed the problem. My code looked like this:
<head runat="server">
<title><asp:ContentPlaceHolder ID="cphPageTitle" runat="server"></asp:ContentPlaceHolder></title>
<link href="_private/Normal.css" rel="stylesheet" type="text/css" />
<script src="_private/GoogleAnalytics.js" type="text/javascript" />
</head>
This caused the problem that I describe in my OP. Everything is fixed when I switch to the following:
<head runat="server">
<title><asp:ContentPlaceHolder ID="cphPageTitle" runat="server"></asp:ContentPlaceHolder></title>
<link href="_private/Normal.css" rel="stylesheet" type="text/css" />
<script src="_private/GoogleAnalytics.js" type="text/javascript"></script>
</head>
Notice that the script
is now closed by a full closing tag rather than a self-closing tag. I assumed that script
could be self-closing with no side effects, but apparently things behave differently when you close it differently. This sheds some light on why.
As keyboardP already suggested, you should put the google analytics script in the <body>
element, preferably at the end (so just before the closing </body>
tag), so it won't block the loading of the page. See http://developer.yahoo./performance/rules.html#js_bottom for a rationale.
Additionally, it's quite possible that the Google Analytics script adds elements (like that <input>
) which are invalid inside the <head>
element, which would explain why the current setup breaks your page so spectacularly.
That's the incorrect way of adding the Google Analytics code. It should look something like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics./ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
...and it should be pasted right before </head>
tag, according to Google:
http://www.google./support/analytics/bin/answer.py?hl=en_US&answer=174090&utm_id=ad
I use Mathias Bynens' method:
<script>
var _gaq = [['_setAccount', 'UA-XXXXX-X'], ['_trackPageview']];
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.async = g.src = '//www.google-analytics./ga.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
</script>
Furthermore, the only reason Google suggests that you place it as high up on the document as possible, is so that you can track a visit that might close out of your page before it fully loads. I don't know who would honestly want to track visitors that don't wait for the page to fully load. That, to me, is not a real visit and would throw off numbers. I place it just before the closing </body>
tag, so that I know all my tracked visitors fully loaded the page.
Just
<script type="text/javascript">
if (!<%=Page.IsPostBack.ToString().Tolower()%>)
{
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-xx']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics./ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
}
</script>
本文标签: javascriptPostback fails on ASPNET page when using Google AnalyticsStack Overflow
版权声明:本文标题:javascript - Postback fails on ASP.NET page when using Google Analytics - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741697164a2393070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论