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;
Good Link for Firefox Click Event Fix
4 comments:
This doesn't work when the onClick uses a Javascipt function....
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);
}
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.
Thanks for the solution.
It was an elegant fix for my issue.
Post a Comment