I have an issue with a page redirect function executing when accessed by the enter key. Basically, onkeypress=Enter or when Search is clicked the page should redirect to a preset url and append a searchstring to the query.
The redirect works if I manually click 'Search', however, if I just hit enter it does not. I added an alert to make sure the search function is firing, which it is, but the document.location.href is not redirecting the page. In FF4, it refreshes the page (but preserves the searchstring). In IE7, it closes the window.
[edit] It seems to be pertinent that I'm using this on a Sharepoint site. The code works fine outside of Sharepoint. [/edit]
The example below simplifies what I have implemented, but recreates the problem.
<script type="text/javascript">
function mySearch() {
var SearchString = document.getElementById("SearchBox").value;
var url = "="+SearchString;
alert(SearchString);
document.location.href = url;
}
</script>
<input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/>
<a id="SearchButton" href="javascript:mySearch();" />Search</a>
Can anybody help?
I have an issue with a page redirect function executing when accessed by the enter key. Basically, onkeypress=Enter or when Search is clicked the page should redirect to a preset url and append a searchstring to the query.
The redirect works if I manually click 'Search', however, if I just hit enter it does not. I added an alert to make sure the search function is firing, which it is, but the document.location.href is not redirecting the page. In FF4, it refreshes the page (but preserves the searchstring). In IE7, it closes the window.
[edit] It seems to be pertinent that I'm using this on a Sharepoint site. The code works fine outside of Sharepoint. [/edit]
The example below simplifies what I have implemented, but recreates the problem.
<script type="text/javascript">
function mySearch() {
var SearchString = document.getElementById("SearchBox").value;
var url = "http://stackoverflow./search?q="+SearchString;
alert(SearchString);
document.location.href = url;
}
</script>
<input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/>
<a id="SearchButton" href="javascript:mySearch();" />Search</a>
Can anybody help?
Share Improve this question edited Apr 27, 2011 at 16:19 Tara asked Apr 27, 2011 at 15:30 TaraTara 411 silver badge3 bronze badges 2-
I copied and pasted your code into a local HTML file (just added
html
tags at top and bottom) and it works for me in Chrome, Firefox, and IE 8. Perhaps the problem is only in the non-simplified version? – Jacob Mattison Commented Apr 27, 2011 at 15:36 - It definitely is a problem for me using this version exactly. If it works elsewhere, perhaps I have an issue with sharepoint, as this is being used on a sharepoint site. – Tara Commented Apr 27, 2011 at 15:42
9 Answers
Reset to default 1try:
window.location = url; // instead of: document.location.href = url;
and you reg code seems to be working here: http://jsfiddle/maniator/RzhXy/
You can replace:
document.location.href = url;
with:
window.document.aspnetForm.action = url;
This has worked for me in SharePoint 2010 websites
I had the same problem with XPages (IBM). I think these frameworks has some problem in mon. What I did is to use the preventDefault() function on the event before triggering the window.location.href.
if(thisEvent.keyCode == 13){ thisEvent.preventDefault(); window.location.href = "newpage.html"; }
I can't explain why it works; I just tried an voila! I hope it helps someone in the future. (Now I can retract the bounty hehehe)
Get rid of the .href. Just set set document.location to change the URL. Your current version should work though...
document.location = url;
Make sure you absolutely have no other action as a response to onkeyup event, in my case the window.onkeyup was calling some other elements onclick handler and everything got messed up.
just try this
<script type="text/javascript">
function mySearch()
{
if(event.keyCode==13)
{
var SearchString = document.getElementById("SearchBox").value;
var url = "http://stackoverflow./search?q="+SearchString;
alert(SearchString);
document.location.href = url;
}
}
</script>
<input id="SearchBox" onkeypress="mySearch();"/>
<a id="SearchButton" href="javascript:mySearch();" />Search</a>
BTW you old function is working fine on my ALL Browser
Manual click works? Then you can have a quick fix solution! If (event.keyCode == 13) document.getElementById('SearchButton').click();
If it doesn't work, try .onclick() too.
Tested the original code in SharePoint 2010 CWEP and it appears to work in IE7, tested in a SharePoint 2007 CEWP and it works in IE9 and FF4
Stab in the dark but you can try removing the inline code for a single function call that has a false return value. I have had problems like that before in SharePoint.
<script type="text/javascript">
function myKeypress(){
if (event.keyCode == 13)
mySearch();
return false;
}
function mySearch() {
var SearchString = document.getElementById("SearchBox").value;
var url = "http://stackoverflow./search?q="+SearchString;
alert(SearchString);
document.location.href = url;
}
</script>
<input id="SearchBox" onkeypress="myKeypress();"/>
<a id="SearchButton" href="javascript:mySearch();">Search</a>
add return false to stop asp form from continuing to process
<script type="text/javascript">
function mySearch() {
var SearchString = document.getElementById("SearchBox").value;
var url = "http://stackoverflow./search?q="+SearchString;
alert(SearchString);
document.location.href = url;
//add return false to stop asp form from continuing to process
return false;
}
</script>
<input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/>
<a id="SearchButton" href="javascript:mySearch();" />Search</a>