Saturday, June 13, 2009

JS: window.event not working in FireFox but works well with IE

There was a search page which takes three inputs and there are two buttons (one for Search and one for AddNew). When the user types something on textbox and presses "Enter" key, the search functionality has to be called. to achieve we used a Javascript Function which will handle the OnKeyPress event for the textbox.

It is working fine in IE whereas in FireFox it is not working and for the enter key press, the AddNew functionality is invoked.

The following is the js function.

function noenter() {
if (window.event.keyCode == 13)
{
document.getElementById('<%=cmdSearch.ClientID%>').click();
return false;
}
}


this is how it is called for onkeypress event of the textbox.



Issue:

The issue is that the event is not recognized by Firefox and IE was able to detect the event with the help of the window.event object.

Workaround:

The workaround will be to pass the event from the textbox to the javascript method. So the change in the javascript function will be as follows

function noenter(e) {
if (e.keyCode == 13)
{
document.getElementById('<%=cmdSearch.ClientID%>').click();
return false;
}
}



and the onkeypress event of the textbox will be as follows



Thus, Firefox is now able to recognize the event. :)

No comments:

Post a Comment