Thursday, April 30, 2009

click Event Problem Firefox Solution

click Event in Firefox and Chrome doesn't work as expected, while it works perfectly with IE.


I have a following Javascript code which was not working
document.getElementById('hlHome').click()
Where "hlHome" is ID of Home Link

Example: I have defined link like following 
<a id="hlHome" runat="server"></a>

Following piece of code was working perfect with IE, but Firefox and Chrome didn't recognise this event.

Solution for Click Event Problem for Firefox, Chrome.
Wherever you are using click event as shown above replace that line with following.
window.location.href = document.getElementById('hlHome').href;

4 comments:

Arend Jan said...

This doesn't work when the onClick uses a Javascipt function....

Anonymous said...

Saw this in a forum, should take care of it:

if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}

wmoecke said...

Sorry Anonymous above me, but the code you posted does nothing. All it does is bind all of a page's controls to a "ghost" .click() event, to make firefox stop complaining about ".click() is not a function" (if u wanna see this, bring up the error console while clicking on the link). It never processes your event handler, because it is being redirected to some "lost in space and time" dummy mocked-up event handler (not yours).
Still, nothing will happen when u click it. I know that, because I've tried it before reaching Ben's solution. I was about to give up at 6:25pm this evening, after struggling with it a whole day.

Unknown said...

Thanks for the solution.

It was an elegant fix for my issue.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape