0

Ok so this should be a fairly easy thing to do however I think I am missing something. I have an input field like so.

<input id="jobSearchField" class="searchField" runat="server" 
onkeypress="javascript:GoToFunc(event, this.value)" type="text" 
onfocus="if (this.value == 'Seach jobs by JobID, JobTitle, Keywords, or Location') this.value=''" 
value="Seach jobs by JobID, JobTitle, Keywords, or Location" />

I also have a button to the right like so.

<button id="filterSubmit" runat="server" class="filterSubmit">Submit</button>

Here is my javascript that I "attempted" to build.

<script type="text/javascript">
    function GoToFunc(e, value) {
        if (e.keyCode == 13) {
            var location = document.location.href + "&query=" + value;
            document.location.href = location;
            document.open();
        }
    }
</script>

My javascript function that I "attempted" to build was suppose to pass that query string back into the url so that I could parse what the user wanted. I am using ASP.NET and C# for the language.

So the question is how do I return back in the url as a parameter what the user typed?

2
  • 1
    An easy method would to just use Response.Redirect within your server side OnClick handler. Commented Sep 5, 2012 at 20:21
  • the label javascript: is useless. Remove it. Commented Sep 5, 2012 at 20:26

2 Answers 2

1

You should either use encodeURI or encodeURIComponent

var location = document.location.href + "&query=" + encodeURI(value);
Sign up to request clarification or add additional context in comments.

Comments

0

First off you are recreating what a form does. If you had just a form it would work.

For your code, stop the enter keys action by returning false. You need to add return to the event handler too. This also expects that the querystring value is not there already.

<input id="jobSearchField" class="searchField" runat="server" 
onkeypress="return GoToFunc(event, this.value)" type="text" 
onfocus="if (this.value == 'Seach jobs by JobID, JobTitle, Keywords, or Location') this.value=''" 
value="Seach jobs by JobID, JobTitle, Keywords, or Location" />



<script type="text/javascript">
    function GoToFunc(e, value) {
        if (e.keyCode == 13) {
            var newLocation = window.location.href + "&query=" + encodeURIComponent(value);
            window.location.href = newLocation;
            return false; //stop event key
        }
    }
</script>

3 Comments

Ok so I tried this approach and after setting window.location.href to newLocation it doesn't actually change the value. Any thoughts?
Wait I added the return for the GoToFunc and then it threw an exception. A potentially dangerous Request.Path value was detected from the client (&).
Um ok, the problem was that the page didn't have its ? before the &. Since this app has multiple page parameters I will have to decipher if the ? is already there plus if there are already parameters added to the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.