Page view counter

Javascript button click event oddity in FireFox

Rate It (1)

Last post 05-08-2009 5:45 PM by lineika. 14 replies.

Sort Posts:

  • Javascript button click event oddity in FireFox

    05-03-2006, 6:58 PM
    • Loading...
    • pinkmuppet
    • Joined on 03-08-2006, 6:00 PM
    • Posts 5
    • Points 25

    Hi Everyone -- This code works fine when outside of an UpdatePanel, but when inside, the server side event never gets called.  It works fine in IE (both inside and outside of an UpdatePanel).

    Any Ideas?  Code to repro below:

    <%

    @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <

    html xmlns="http://www.w3.org/1999/xhtml" >

    <

    head runat="server"></head>

    <

    body>

    <form id="form1" runat="server">

    <atlas:ScriptManager EnablePartialRendering="true" runat="server" />

    <atlas:UpdatePanel ID="mainUpdatePanel" Mode="Always" runat="server">

    <ContentTemplate>

    <div style="display:none;"><asp:Button ID="btn" runat="server" Text="thebutton" /></div>

    <a href="" id="javascriptBtnInvoke" runat="server">javascript button click</a>

    </ContentTemplate>

    </atlas:UpdatePanel>

    </form>

    </

    body>

    </

    html>

     

     

    public

    partial class _Default : System.Web.UI.Page

    {

    protected void Page_Init(object sender, EventArgs e)

    {

    btn.Click +=

    new EventHandler(btn_Click);

    }

    protected void Page_Load(object sender, EventArgs e)

    {

    javascriptBtnInvoke.HRef =

    "javascript:document.getElementById('" + btn.ClientID + "').click();";

    //also tried this -- doesn't work in our out of a update panel

    //javascriptBtnInvoke.HRef = "javascript:__doPostBack('" + btn.ClientID + "','');";

    }

    void btn_Click(object sender, EventArgs e)

    {

    }

    }

  • Re: Javascript button click event oddity in FireFox

    05-04-2006, 9:08 AM
    • Loading...
    • Luis Abreu
    • Joined on 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • Points 25,660
    • TrustedFriends-MVPs

    hello.

    yes, firefox handles things differently than IE.

    the problem occurs on the method _onFormElementClick. you see, when you call the click method programatically, you'll get different results in IE and firefox. to IE, the window.event.srcElement points to the button; however, in firefox, it points to the javascript code you've added to the href attribute.

    to be honest, i don't have a clue on which type of behavior is correct. what i'd suggest is opening a bug entry by adding a new post on this forum with the word [BUG] on the title.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Javascript button click event oddity in FireFox

    05-10-2006, 7:14 PM
    • Loading...
    • danman_5
    • Joined on 01-15-2003, 3:35 PM
    • Posts 6
    • Points 30

    Hi there, I am experiencing the exact same error with my application, did you come up with any workaround for this problem?

    For me I want to be able to Trigger the update for a specific update panel, but firefox updates them all if the click event is triggered in javascript...

    I would be very interested if you had any suggestions...

    Thanks,
    Dan

  • Re: Javascript button click event oddity in FireFox

    05-24-2006, 11:05 AM
    • Loading...
    • Garbin
    • Joined on 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,501
    • Points 7,396
    • ASPInsiders
      TrustedFriends-MVPs
    Hi,

    solution: declare the hidden button with UseSubmitBehavior="false": this will make the button postback with the ASP.NET postback model and this should do the trick.
    <div style="display:none;">
    <asp:Button id="btnHidden" runat="server" OnClick="btnHidden_Click" UseSubmitBehavior="false" /> </div>
     
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: Javascript button click event oddity in FireFox

    06-11-2006, 5:30 AM
    • Loading...
    • danman_5
    • Joined on 01-15-2003, 3:35 PM
    • Posts 6
    • Points 30
    Thanks Garbin, worked a treat!
  • Re: Javascript button click event oddity in FireFox

    10-09-2006, 9:51 PM
    • Loading...
    • Eilon
    • Joined on 06-26-2002, 2:14 PM
    • Redmond, WA
    • Posts 866
    • Points 4,828
    • AspNetTeam

    I think that what happens in Firefox is that when you call click() on the button it simply invokes all the event handlers and doesn't really do a click operation. And I'm also pretty sure that according to W3C that IE has the correct behavior by truly simulating a click operation. We don't have any plans to make this oddity work with Atlas since there appear to be workarounds. Also I'm not really sure why you'd want to explicitly click a button - it would seem to me that that is just to workaround issues in the app.

    Thanks,

    Eilon

    Blog: http://weblogs.asp.net/LeftSlipper/
  • Re: Javascript button click event oddity in FireFox

    02-23-2007, 2:52 PM
    • Loading...
    • haphazard
    • Joined on 02-22-2007, 3:03 PM
    • Posts 2
    • Points 4
    I was having the same problem.  Lost half a day trying to figure it out.  Just wanted to say thanks.
  • Re: Javascript button click event oddity in FireFox

    03-29-2007, 1:40 PM
    • Loading...
    • WishStar99
    • Joined on 08-05-2006, 12:11 AM
    • VietNam
    • Posts 552
    • Points 1,862

    Garbin, thanks for the solution. Cheer

    //---------------------------------------------//
    //------------------- Chloé ------------------//
    //---------------------------------------------//
  • Re: Javascript button click event oddity in FireFox

    04-10-2007, 8:10 AM
    • Loading...
    • darwinc
    • Joined on 02-01-2006, 7:34 PM
    • Posts 2
    • Points 7

    e.g. its a work around for fire a button when hitting enter in a textbox (searchBox...)

    thx a lot! 

  • Re: Javascript button click event oddity in FireFox

    09-18-2007, 6:28 AM
    • Loading...
    • giles.hinton
    • Joined on 09-18-2007, 10:21 AM
    • Posts 6
    • Points 4

    This is because IE automatically holds the event object at the window level, whereas Mozilla does not as far as I'm aware.

    Whereas in IE you can fire an event, and in the receiving function, identify the caller using event.srcElement, Mozilla requires you to pass the event object as part of the argument in order to identify the caller.  This simply means that instead of using runFunction(), you use runFunction(event).

    You can then pick up the caller in Mozilla using event.target.

    My experience would lead me to suggest that the window.event property in IE is one of those "Microsoft-only" implementations, and not adherent to any W3C standard; whilst this isn't always a bad thing, Microsoft shouldn't be using it in what is supposed to be a truly "cross browser" capable set of extensions.

    Just my two-pennies' worth - I'd be grateful if anyone knows if Mozilla plan to change their model in the future, as to be fair it is a pain to have to pass around the event object.

  • Re: Javascript button click event oddity in FireFox

    02-04-2008, 3:03 AM
    • Loading...
    • yizharm
    • Joined on 02-04-2008, 8:01 AM
    • Posts 1
    • Points 2

    Hi,

     It's work very good, Thank you very much!!!

     

  • Re: Javascript button click event oddity in FireFox

    04-04-2008, 6:51 PM
    • Loading...
    • lineika
    • Joined on 05-29-2007, 3:26 AM
    • Posts 7
    • Points 4

    hello guys I dont know if anybody still needs help but i found this to work:

     

    if ($get("buttonName").dispatchEvent)

    {

    var e = document.createEvent("MouseEvents");

    e.initEvent("click", true, true);

    $get("buttonName").dispatchEvent(e);

    }

    else

    {

    $get(
    "buttonName").click();

    }

  • Re: Javascript button click event oddity in FireFox

    05-19-2008, 2:27 PM
    • Loading...
    • zyxockjm
    • Joined on 12-16-2003, 9:09 PM
    • Posts 37
    • Points 153

    lineika, thank you so much for your post. That solved my problem for my firefox issue. I was trying to look for some sort of onclick() event because click() wasn't working in firefox.

  • Re: Javascript button click event oddity in FireFox

    05-08-2009, 4:59 PM
    • Loading...
    • praveennerd
    • Joined on 05-08-2009, 8:57 PM
    • Posts 1
    • Points 2
    Thanks Lineika ... This indeed worked in my case where I must stimulate the click event as many validations were attached to it and eval was kind of useless to me. Once again thanks for the perfect solution you provided.
  • Re: Javascript button click event oddity in FireFox

    05-08-2009, 5:45 PM
    • Loading...
    • lineika
    • Joined on 05-29-2007, 3:26 AM
    • Posts 7
    • Points 4

    You are very very welcome! Glad I could help!

Page 1 of 1 (15 items)