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
    • Member
      25 point Member
    • pinkmuppet
    • Member since 03-08-2006, 6:00 PM
    • Posts 5

    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
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • 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
    • Member
      30 point Member
    • danman_5
    • Member since 01-15-2003, 3:35 PM
    • Posts 6

    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
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • 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
    • Member
      30 point Member
    • danman_5
    • Member since 01-15-2003, 3:35 PM
    • Posts 6
    Thanks Garbin, worked a treat!
  • Re: Javascript button click event oddity in FireFox

    10-09-2006, 9:51 PM
    • Contributor
      5,514 point Contributor
    • Eilon
    • Member since 06-26-2002, 6:14 PM
    • Redmond, WA
    • Posts 945

    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
    • Member
      4 point Member
    • haphazard
    • Member since 02-22-2007, 3:03 PM
    • Posts 2
    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
    • Participant
      1,862 point Participant
    • WishStar99
    • Member since 08-05-2006, 12:11 AM
    • VietNam
    • Posts 552

    Garbin, thanks for the solution. Cheer

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

    04-10-2007, 8:10 AM
    • Member
      7 point Member
    • darwinc
    • Member since 02-01-2006, 7:34 PM
    • Posts 2

    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
    • Member
      104 point Member
    • giles.hinton
    • Member since 09-18-2007, 10:21 AM
    • Posts 29

    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
    • Member
      2 point Member
    • yizharm
    • Member since 02-04-2008, 8:01 AM
    • Posts 1

    Hi,

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

     

  • Re: Javascript button click event oddity in FireFox

    04-04-2008, 6:51 PM
    • Member
      4 point Member
    • lineika
    • Member since 05-29-2007, 3:26 AM
    • Posts 7

    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
    • Member
      153 point Member
    • zyxockjm
    • Member since 12-16-2003, 9:09 PM
    • Posts 37

    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
    • Member
      2 point Member
    • praveennerd
    • Member since 05-08-2009, 8:57 PM
    • Posts 1
    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
    • Member
      4 point Member
    • lineika
    • Member since 05-29-2007, 3:26 AM
    • Posts 7

    You are very very welcome! Glad I could help!

Page 1 of 1 (15 items)