views:

232

answers:

2

I am trying to implement a search functionality that is capturing the enter key and redirecting to a different page in an ASP.NET 3.5 application. Unfortunately it does not work in Firefox (version 3.5) but in IE it is working perfectly. Please see the following code:

Script:

function searchKeyPress(e) {
  if (window.event) { e = window.event; }
  if (e.keyCode == 13) {
    document.getElementById('btnSearch').click();
  }
}
function redirect() {
  document.location = "http://localhost:5555/search.aspx?q=keyword";
}

Markup:

  <form name="form1" method="post" runat="server" id="form1"/>
     <input type="text" id="txtSearch" onkeypress="searchKeyPress(event);"/>
     <input type="button" id="btnSearch" Value="Search" onclick="redirect();"/>
  </form/>

Has anyone else experienced this issue?

Any help would be appreciated!

A: 
  <script type="text/javascript">
    function searchKeyPress(e) {
        if (window.event) { e = window.event; }
        if (e.keyCode == 13) {
            document.getElementById('form1').submit();
        }
    }
    function redirect() {
       document.location = "http://localhost:5555/search.aspx?q=keyword";
    }
</script>

Use forms.submit() instead of .click(), .click() is only supported on ie, submit is supported in firefox also.

CodeJoust
No It did not work !!!!
Deepu
+1  A: 

Why don't you use a Submit button along with a form action to take you to the search page? The submit button exhibits the behavior you are looking for by default, so there is no need for javascript.

<form name="form1" method="get" action="/search.aspx" id="form1"/>
    <input type="text" id="q" />
    <input type="submit" id="btnSearch" Value="Search" />
</form/>

If you really want to stick with your javascript solution (which I don't recommend because it is less accessible and relys on javascript) give this a try

function searchKeyPress(e) {
  e = e || window.event || event;
  var code = e.charCode || e.keyCode || e.which;
  if (code == 13) {
    redirect();
  }
}
Bob
Thanks for your suggestion, Well I am looking some thing a page redirection with Query String values (similar to get method) I don't need a full form post
Deepu
Give this a try, the `get` method will post the values in the querystring.
Bob