English 中文(简体)
Javascript problem with location.href
原标题:

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.

最佳回答

I m not sure you need all of this. Normal, built-in behavior for the following HTML markup does exactly as you want without the need for any JavaScript:

<form method="get" action="/Search/">
    <input type="text" name="Keywords">
</form>

However, it is important that your UI have some sort of way to submit the form that doesn t involve a key stroke. This is for two reasons:

  1. As Jorn pointed out, it might not be readily apparent that the key submits the form.
  2. Having no submit button may break usability for users who use alternative input devices.

Because of the above reasons, the following is suggested:

<form method="get" action="/Search/">
    <input type="text" name="Keywords">
    <input type="submit" value="Search or  go  or whatever">
</form>
问题回答

Give this a try, I just tested this locally, it works on my machine

function transferUser(url) {
    window.location = url;
}

And you will want to change how you construct your URL to the following

var searchCriteria = 
    escape(document.getElementById( <%= searchBox.ClientID %> ).value); 
var searchLink =  /Search/?Keywords=  + searchCriteria




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签