Document.location.href does not update onkeypress in sharepoint

I have a problem with the page redirection function that is executed when accessed by the enter key. Basically, onkeypress = Enter, or when you click on search, the page should be redirected to a predefined URL and add the search string to the request.

Redirecting works if I manually click "Search", however, if I just got into it, it is not. I added a warning to make sure the search function is activated, but this document.location.href does not redirect the page. In FF4, it refreshes the page (but saves the search bar). In IE7, it closes the window.

[ edit ] It seems appropriate that I use this on the Sharepoint site. The code works fine outside of Sharepoint. [/ change ]

The example below simplifies what I implemented but recreates the problem.

<script type="text/javascript"> function mySearch() { var SearchString = document.getElementById("SearchBox").value; var url = "http://stackoverflow.com/search?q="+SearchString; alert(SearchString); document.location.href = url; } </script> <input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/>&nbsp; <a id="SearchButton" href="javascript:mySearch();" />Search</a> 

Does anyone help?

+4
source share
9 answers

to try:

 window.location = url; // instead of: document.location.href = url; 

and you reg code seem to be working here: http://jsfiddle.net/maniator/RzhXy/

+1
source

You can replace:

 document.location.href = url; 

with:

 window.document.aspnetForm.action = url; 

This worked for me on SharePoint 2010 websites

+1
source

I had the same issue with XPages (IBM). I think this framework has a common problem. What I did was use the preventDefault () function in the event before running window.location.href.

  if (thisEvent.keyCode == 13) {
     thisEvent.preventDefault ();
     window.location.href = "newpage.html";
 }

I can not explain why this works; I just tried voila! Hope this helps someone in the future. (Now I can remove the hehehe generosity)

+1
source

Get rid of .href. Just set document.location to URL. Your current version should work though ...

 document.location = url; 
0
source

just try this

 <script type="text/javascript"> function mySearch() { if(event.keyCode==13) { var SearchString = document.getElementById("SearchBox").value; var url = "http://stackoverflow.com/search?q="+SearchString; alert(SearchString); document.location.href = url; } } </script> <input id="SearchBox" onkeypress="mySearch();"/>&nbsp; <a id="SearchButton" href="javascript:mySearch();" />Search</a> 

By the way, you old function works fine in my browser ALL

0
source

I tested the source code in CWEP SharePoint 2010 and it seems to work in IE7, it is tested in CEWP SharePoint 2007 and it works in IE9 and FF4

A hit in the dark, but you can try removing the inline code for a single function call with a false return value. I used to have problems 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.com/search?q="+SearchString; alert(SearchString); document.location.href = url; } </script> <input id="SearchBox" onkeypress="myKeypress();"/> <a id="SearchButton" href="javascript:mySearch();">Search</a> 
0
source

Make sure that you absolutely have no other action as a response to the onkeyup event, in my case window.onkeyup called some other elements of the onclick handler, and everything got messed up.

0
source

Does manual click work? Then you can quickly solve the problem! If (event.keyCode == 13) document.getElementById ('SearchButton'). Click ();

If this does not work, try also .onclick ().

0
source

add return false to stop the asp.net form from continuing the process

 <script type="text/javascript"> function mySearch() { var SearchString = document.getElementById("SearchBox").value; var url = "http://stackoverflow.com/search?q="+SearchString; alert(SearchString); document.location.href = url; //add return false to stop asp.net form from continuing to process return false; } </script> <input id="SearchBox" onkeypress="if (event.keyCode == 13) mySearch();"/>&nbsp; <a id="SearchButton" href="javascript:mySearch();" />Search</a> 
0
source

All Articles