Javascript problem with location.href.
Posted
by Patrick
on Stack Overflow
See other posts from Stack Overflow
or by Patrick
Published on 2009-11-23T19:31:21Z
Indexed on
2010/06/09
7:12 UTC
Read the original article
Hit count: 310
Hello!
I have a textbox and whenever the user presses a key it's checked to see if the user pressed enter. If the enter key is pressed, i want to add all the info in the textbox and transfer the user to a different url.
<script language="javascript" type="text/javascript">
function checkEnter(e){ //e is event object passed from function invocation
var characterCode;
if(e && e.which){ //if which property of event object is supported (NN4)
e = e;
characterCode = e.which; //character code is contained in NN4's which property
}
else{
e = event;
characterCode = e.keyCode; //character code is contained in IE's keyCode property
}
if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
var searchLink = '/Search/?Keywords=' + document.getElementById('<%= searchBox.ClientID %>').value;
transferUser(searchLink);
return false;
}
else{
return true;
}
}
function transferUser(url) {
window.location.href = url;
window.location.replace(url);
}
</script>
Search: <input name="ctl00$searchBox" type="text" id="ctl00_searchBox" class="header_line_search_box_textbox" onKeyPress="checkEnter(event);" />
I have tried every possible combination, but nothing happens. The site just refreshes itself.
I also need somehow to convert the text from the user to html safe, must like the HttpUtility.EncodeUrl in aspx.
© Stack Overflow or respective owner