redirect user to current page with some querystring using javascript

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 = "https://stackoverflow.com/questions/1309483/?search=" + escape( $("#someId").val());

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

function reloadWithQueryStringVars (queryStringVars) {
    var existingQueryVars = location.search ? location.search.substring(1).split("&") : [],
        currentUrl = location.search ? location.href.replace(location.search,"") : location.href,
        newQueryVars = {},
        newUrl = currentUrl + "?";
    if(existingQueryVars.length > 0) {
        for (var i = 0; i < existingQueryVars.length; i++) {
            var pair = existingQueryVars[i].split("=");
            newQueryVars[pair[0]] = pair[1];
        }
    }
    if(queryStringVars) {
        for (var queryStringVar in queryStringVars) {
            newQueryVars[queryStringVar] = queryStringVars[queryStringVar];
        }
    }
    if(newQueryVars) { 
        for (var newQueryVar in newQueryVars) {
            newUrl += newQueryVar + "=" + newQueryVars[newQueryVar] + "&";
        }
        newUrl = newUrl.substring(0, newUrl.length-1);
        window.location.href = newUrl;
    } else {
        window.location.href = location.href;
    }
}

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 variable foo whose value should be bar, I’d call the function as follows:

reloadWithQueryStringVars({"foo": "bar"});

The browser will navigate to http://localhost/helloworld.php?foo=bar. And If I pass the function the following:

reloadWithQueryStringVars({"foo": "bar2"});

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 follows

reloadWithQueryStringVars({"foo": "bar3", "answer": "42", "bacon": "tasty"});

The 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:

reloadWithQueryStringVars({"search": escape( $("#someId").val() )});

window.location.href = window.location.href + "https://stackoverflow.com/questions/1309483/?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?

<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.

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 = "https://stackoverflow.com/questions/1309483/?search=" + encodeURIComponent( $("#someId").val());

you need to prepend the actual base url

window.location.href = window.location.href + "https://stackoverflow.com/questions/1309483/?search=" + escape( $("#someId").val());

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

         window.location = "https://stackoverflow.com/questions/1309483/?search=" + encodeURIComponent($("#someId").val());

         //this line of code is intended for older ie and might work,
         //because I don't remember it exactly
        e.stopPropagation();

         return false;
     });                                                                   

});

What part isn’t working?

Try encodeURI() rather than escape().

$("a").click(function() {

 document.location.href += "https://stackoverflow.com/questions/1309483/?search=" + encodeURIComponent($("#myTextBox").val());


});

    function Redirect()
      {
    window.location.href = "https://stackoverflow.com/questions/1309483/URL_TO_BE_Redircted";
      }

then In button (asp.net)

    <asp:Button runat="server" id="btnRedirect" onClientClick="Redirect();return false;"/>

So after clicking on the button the control will go at serverside , and there
you can access the Querystring you form !


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts