views:

565

answers:

8

Hi,

When a person clicks on a link, I want to do this:

  1. grab the text from a textbox
  2. encode the text
  3. redirect to currentpage.aspx?search=textboxvalue

I have this so far but its not working:

window.location.href = "?search=" + escape( $("#someId").val());
A: 

What part isn't working?

Try encodeURI() rather than escape().

great_llama
+1  A: 

you need to prepend the actual base url

window.location.href = window.location.href + "?search=" + escape( $("#someId").val());
Jaime
+4  A: 

window.location.href = window.location.href + "?search=" + escape( $("#someId").val()); ?

Zed
If the URL already has a querystring, won't this add to it rather than replace it? You'll end up with multiple ?search segments.
great_llama
+1  A: 

How about:

window.location = "/currentpage.aspx?search=" + escape( $("#someId").val());

or

window.location = window.location.pathname + "?search=" + escape( $("#someId").val());

Etc...

Kenny
A: 
$("a").click(function() {

 document.location.href += "?search=" + encodeURIComponent($("#myTextBox").val());


});
Marwan Aouida
A: 

$(document).ready(function(){ $('a').click(function(e){

    window.location = "?search=" + encodeURIComponent(

$("#someId").val());

    //this line of code is intended for older ie and might work,
    //cuz i dont remember it exactly
    e.stopPropagation();

    return false;
}); });
Funky Dude
+2  A: 

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?

<form id="search" method="get">
<input type="text" name="search">
</form>
<a href="#" id="go">Search</a>

with:

$(function() {
  $("#go").click(function() {
    $("#search").submit();
    return false;
  });
});

and then you don't have to worry about the right URL, encoding, etc.

cletus
A: 

You are changing the wrong location property if you only want to change the search string. I think you want to do this:

location.search = "?search=" + encodeURIComponent( $("#someId").val());
Eli Grey