Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onkeydown does not redirect page using location.href

Status
Not open for further replies.

ksh5u

Programmer
Joined
Apr 12, 2006
Messages
2
Location
US
Hi,

I am trying to use javascript to allow the user to press enter on a search input field and have the page redirect to the search result page. So after the user enters a text, instead of having to click "Search" with the mouse, the user will press enter and the whole page will redirect to the result page. However, I am getting a weird error. After I click enter, the page merely refreshes and does not redirect ot the new page. BUT if I put a window.alert("this is a text") in teh function that redirects, it alerts then redirects fine. I can't figure out why its doing that. Here is the javascript code that does the redirect and below that the onkeydown code for the input field. Any help is greatly appreciated.

<script language="javascript">

var servername = location.host;
function SearchcBase()
{
if(document.all.item("txtcBaseSearch").value != "")
{
Redirect_oIFrame(" + servername + "/Document%20Library/SearchResult.aspx?keyword="+document.all.item("txtcBaseSearch").value);
}
}
function Redirect_oIFrame(url)
{
window.location.href = url;
}
function DetectKeyEvent(button)
{
if(window.event.keyCode == 13)
{
SearchcBase();
}
}
</script>

<INPUT class="cBaseTextBox" id="txtcBaseSearch" type="text" onKeyDown="DetectKeyEvent('hrefSearchcBase')">
 
You need to:

- Put "return(false)" in your form's onsubmit handler to stop the form submission
- Add in code to make it cross-browser compatible (window.event is IE-only).

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan but I am actually using the code in an ascx page for a sharepoint web part. So I dont have any form tags on the page. Just the javascript code and TABLE.

This is all I have

<script language="javascript">

var servername = location.host;

function SearchcBase()
{
if(document.all.item("txtcBaseSearch").value != "")
{
Redirect_oIFrame(" + servername + "/Document%20Library/SearchResult.aspx?keyword="+document.all.item("txtcBaseSearch").value);
}
}
function Redirect_oIFrame(url)
{
window.location.href = url;
//window.alert("test");
}
function DetectKeyEvent(button)
{
if(window.event.keyCode == 13)
{
SearchcBase();
}
}
</script>
<TABLE id="Table2" cellSpacing="0" cellPadding="1" border="0" width="1%">
<TR>
<TD><INPUT class="cBaseTextBox" id="txtcBaseSearch" type="text" onKeyDown="DetectKeyEvent('hrefSearchcBase')"></TD>
</TR>
<TR>
<TD vAlign="top" align="left">
<TABLE id="Table3" cellSpacing="0" cellPadding="1" border="0">
<TR>
<TD><IMG class="cBaseImagePadding" alt="" src="_layouts/Images/search.jpg"></TD>
<TD><A id="hrefSearchcBase" href="#" class="cBaseActionLinks" onclick="SearchcBase()">Search</A></TD>
</TR>
<TR>
<TD><IMG class="cBaseImagePadding" alt="" src="_layouts/Images/imgReport.jpg"></TD>
<TD nowrap><a class="cBaseActionLinks" href="go somewhere">Referring
Physician Lookup</a></TD>
</TR>
<TR>
<TD><IMG class="cBaseImagePadding" alt="" src="_layouts/Images/imgReport.jpg"></TD>
<TD nowrap><a class="cBaseActionLinks" href="go somewhere else">Report(s)</a></TD>
</TR>
<TR>
<TD><IMG class="cBaseImagePadding" alt="" src="_layouts/Images/imgSearch.jpg"></TD>
<TD nowrap><A class="cBaseActionLinks" href="go some other place">Advanced
Search</A></TD>
</TR>
<TR>
<TD><IMG class="cBaseImagePadding" alt="" src="_layouts/Images/imgHelp.jpg"></TD>
<TD nowrap><A class="cBaseActionLinks" href="no where else to go">Request
For Change</A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
 
[1] Try add a returnValue=false here see what happens.
[tt]
if(window.event.keyCode == 13)
{
SearchcBase();
window.event.returnValue=false;
}
[/tt]
[2] Also as an aside, not directly related to your problem, the query string should be encoded, should it not? Or would it be done automatically? A minimal measure I would take is this.

>[tt]Redirect_oIFrame(" + servername + "/Document%20Library/SearchResult.aspx?keyword="+document.all.item("txtcBaseSearch").value);[/tt]

[tt]var s=document.all.item("txtcBaseSearch").value;
s=escape(s.replace(/ /g,"+"));
Redirect_oIFrame(" + servername + "/Document%20Library/SearchResult.aspx?keyword="+s);
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top