1

Sorry for the duplication of my post Why does setTimeout fire twice in Chrome, but not IE or Firefox?.

I actually need the following javascript call to work inline for the hyperlinks, so I was wondering if anyone has any ideas. Clicking on either of the hyperlinks causes the javascript function GenerateNewNumber to fire twice in Chrome, but not IE or Firefox. Once you've clicked on it once in Chrome, it only adds to the label once on subsequent clicks. Again I'm using Chrome 20.0.1132.57, IE9 and Firefox 13.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" language="javascript">

    function GenerateNewNumber() {
        var randomnumber = Math.floor(Math.random() * 1234);
        document.getElementById('NumberLabel').innerHTML += '<br>' + 'Random Number is ' + randomnumber;
    }

</script>
</head>
<body>
    <form id="form1" runat="server">

    <asp:LinkButton ID="Button1" runat="server" OnClientClick="GenerateNewNumber(); return false;">Click for Random Number Test 1</asp:LinkButton><br />
    <a id="Button2" href="#" onclick="GenerateNewNumber(); return false;" target="_blank">Click for Random Number Test 2</a>
    <asp:Label ID="NumberLabel" runat="server"></asp:Label>
    </form>
</body>
</html>

Thanks again for your help.

Community
  • 1
  • 1
user199962
  • 41
  • 1
  • 3
  • possible duplicate of [Why does setTimeout fire twice in Chrome, but not IE or Firefox?](http://stackoverflow.com/questions/11596982/why-does-settimeout-fire-twice-in-chrome-but-not-ie-or-firefox) –  Jul 22 '12 at 02:19
  • Yes, it's similar, but the problem in this example isn't with setTimeout, plus I need it to execute inline as opposed to the jQuery example I provided. – user199962 Jul 22 '12 at 02:34
  • http://jsfiddle.net/HUh8E/ I have no such problems on Chrome 20 or 21. Can you get a failing test case without any ASP stuff at all? – ephemient Jul 22 '12 at 05:00

1 Answers1

0

This could be a result of a google extension you're using. However, I do recall having this issue and with the return false specified in the onclick of a hyperlink. I remember some mundane tweak got it to stop firing off twice.

Try removing the semi-colon behind 'return false'.

Reviewing this article, asp.NET LinkButton not working in Google Chrome

You can try this method.

protected override void OnPreInit(EventArgs e)
{
    if (Request.UserAgent != null && (Request.UserAgent.IndexOf("AppleWebKit") > 0))  // added for compatibility issues with chrome 
    {
         this.ClientTarget = "uplevel";
    }

     base.OnPreInit(e);
}

In the end, you should consider using elements/tags other than links/anchors.

Community
  • 1
  • 1
Brett Caswell
  • 1,486
  • 1
  • 13
  • 25
  • Thanks Brett - it was an extension causing the problem (Double Click). I will definitely look out for this in the future. – user199962 Jul 23 '12 at 06:04