When a person clicks on a link, I want to do this:
- grab the text from a textbox
- encode the text
- redirect to currentpage.aspx?search=textboxvalue
I have this so far but its not working:
window.location.href = "?search=" + escape( $("#someId").val());
The problem with appending something to
window.location.href
is what if you have already done that? You'll just keep appending"?search=..."
multiple times. More importantly, it's more complicated than it needs to be.You're already using jQuery. Why not just do this?
with:
and then you don't have to worry about the right URL, encoding, etc.
});
I needed to do something similar to what you wanted so I put together a function that allows you to do it. I hope it is found useful. I have tested it pretty extensively and not found any issues, please don't hesitate to let me know if any issues are found.
The Function
How to Use It
The function accepts an object literal as its only argument. The object literal should contain the query string variable(s) that you want to reload the page with. It accounts for existing variables and will override an existing one if you specify it in the object literal you pass.
So, lets say our location is
http://localhost/helloworld.php
and I want to redirect to the current page with a query string variablefoo
whose value should bebar
, I'd call the function as follows:The browser will navigate to
http://localhost/helloworld.php?foo=bar
. And If I pass the function the following:The browser will navigate to
http://localhost/helloworld.php?foo=bar2
.As the function accepts a object literal, you can pass multiple properties to the function for multiple query string vars. If I'm still at
http://localhost/helloworld.php?foo=bar2
and I call function as followsThe browser will navigate to
http://localhost/helloworld.php?foo=bar3&answer=42&bacon=tasty
Your Use Case
In answer to the question however, you'd call the function as follows:
How about:
or
Etc...