最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onclick event not working in Internet Explorer - Stack Overflow

programmeradmin4浏览0评论

I'm using asp to add add a client side onclick event to a span tag in a ListView:

<span id="mySpan" onclick="<%# Request.Cookies["myCookie"] != null ? "MyFunction1(" + Eval("MyVal1") + ");" : "MyFunction2(" + Eval("MyVal2") + ");" %>"

Every aspect of what I'm doing always works perfectly in Chrome and FF 100% of the time. In IE (v8 and v9), it always adds the correct javascript function and all of the code looks perfect - the rendered span code in IE looks identical to how it looks in Chrome and FF. However, when monitoring traffic with Fiddler, several random IE spans don't trigger the javascript function at all. Some do. There is no difference in the code between ones that work and ones that do not work. The ones that don't work in IE work fine in Chrome and FF. I am also able to confirm the javascript function isn't being fired by using means other than Fiddler, so Fiddler isn't giving me false readings.

Is there something I'm doing in my asp code to add the function that may be screwing this up in IE?

I'm using asp to add add a client side onclick event to a span tag in a ListView:

<span id="mySpan" onclick="<%# Request.Cookies["myCookie"] != null ? "MyFunction1(" + Eval("MyVal1") + ");" : "MyFunction2(" + Eval("MyVal2") + ");" %>"

Every aspect of what I'm doing always works perfectly in Chrome and FF 100% of the time. In IE (v8 and v9), it always adds the correct javascript function and all of the code looks perfect - the rendered span code in IE looks identical to how it looks in Chrome and FF. However, when monitoring traffic with Fiddler, several random IE spans don't trigger the javascript function at all. Some do. There is no difference in the code between ones that work and ones that do not work. The ones that don't work in IE work fine in Chrome and FF. I am also able to confirm the javascript function isn't being fired by using means other than Fiddler, so Fiddler isn't giving me false readings.

Is there something I'm doing in my asp code to add the function that may be screwing this up in IE?

Share Improve this question asked Aug 2, 2011 at 14:56 StronglyTypedStronglyTyped 2,1345 gold badges29 silver badges49 bronze badges 4
  • It sounds like the problem is occurring client-side. Can you provide an example of the HTML tag that gets produced? Or better yet, a working jsfiddle that reflects the behavior you're describing? – StriplingWarrior Commented Aug 2, 2011 at 15:18
  • here is an example of the rendered code that is not working: <span id="mySpan" onclick="MyFunction1(257, 1)">Text</span> (i've tried adding a trailing ; and a return to the function, both didn't work, as I expected). The code that does work looks exactly the same. The Fiddler response looks as it should when it works, but there is no action registered in Fiddler at all when it doesn't work. The event simply never gets fired. – StronglyTyped Commented Aug 2, 2011 at 15:38
  • If you change the onclick to alert('hi');, does it have the same behavior? – StriplingWarrior Commented Aug 2, 2011 at 15:52
  • alert('hi'); works correctly. it must be an issue with the dynamic generation... just not sure what. or why its so random. – StronglyTyped Commented Aug 2, 2011 at 16:29
Add a ment  | 

2 Answers 2

Reset to default 2

I'm guessing this is a problem with the javascript functions you're running. Try putting alert messages at various points in Function1's execution to figure out where it is breaking. You probably have javascript code that isn't cross-browser patible, but which only gets hit for certain elements.

It probably doesn't like that hash # in there and is menting out the JS code.

Try placing it in a script tag:

<span id="mySpan">

<script type="text/javascript">
    document.getElementById("mySpan").addEventListener("click",function(){
    <% if(Request.Cookies["myCookie"] != null)
       {
    %>
           MyFunction1(MyVal1);
    <%
       }
       else
       {
    %>
           MyFunction2(MyVal2);
    <%
       }
    %>
    }
</script>

I apologize if my ASP syntax is off. I haven't coded it in years but hopefully you'll get the idea. Any ASP coders please feel free to fix this code for me ;)

发布评论

评论列表(0)

  1. 暂无评论