1

Let's say I'm on a page on my website: http://www.example.com/SearchResults.asp?Search=stackoverflow. Is it possible to use a JavaScript or jQuery code to get the variable "search" from the URL string and populate it into the value of the search box?

Basically I'll have my regular identified search box:

<input type="text" title="Search" id="search_box" />

And a user will search for something, but I will want a JavaScript or jQuery code that will get the value from the "search" variable in the URL string when the customer is on the Search.asp page and add it as the "value" to the input#search.

So the user will be on this page: http://www.example.com/SearchResults.asp?Search=stackoverflow and the search box will look like:

<input type="text" title="Search" id="search_box" value="stackoverflow" />

Thanks

3 Answers 3

2

You could try this function:

function getSearchVariable(variable) 
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){
            return unescape(pair[1]);
        }else{
            return false;
        }
    }
}

If this function is present in the sample you mentioned above, all you would call getSearchVariable("Search") to have "stackoverflow" returned.

So in your page, you would have a script element that looks like:

<script type="text/javascript">
    function getSearchVariable(variable) 
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){
                return unescape(pair[1]).split("+").join(" ");
            }else{
                return "";
            }
        }
    }

    $(document).ready(function(){
        $("#search_box").val(getSearchVariable("Search"));
    });
</script>

Hope that helps!

Sign up to request clarification or add additional context in comments.

9 Comments

This is also another nice function: (And a little less verbose than mine) css-tricks.com/snippets/javascript/get-url-variables
I realized I cannot edit these since I barely know anything about JavaScript. The actual pages and id's are different that what I initially posted. The ID of the search input is actually search_box and the page is actually SearchResults.asp. What do I change in the code above to make it work for me?
Wow this works excellently. One problem is when there is nothing in the search box, it says false. I just changed return false; to return ; and it worked! Another thing is the URL string changes spaces to "+" how can I have the script change the "+" back into spaces.
Ahh yeah, I had left the false in there for error checking; replacing it with return ""; if the best bet. I've updated the code to replace pluses with spaces.
Thanks, the pluses are still returning though. Also, another bothersome question, how can I have it only match "Search" I also have the variable "Searching" and when I'm on those pages I see "hing" in the search box.
|
1

You can use the JQuery URI plugin (shamelessly written by yours truly) to extract any piece of the URL: Specifically, $.uri(window.location.href).at("query").search yields "stackoverflow" for the said URL.

The overall flow would be to register a callback (fired on page-load) to analyze the URL and set the value of that form element, that is:

<script>
  $(document).ready(function () {  
    $('#Search').val($.uri(window.location.href).at("query").search); 
  })
</script>

Comments

0

JavaScript

document.getElementById('Search').value = [(window.location.search.match(/(?:^|[&;])Search=([^&;]+)/) || [])[1]];

jsFiddle.

The wrapping it with [] means that if the value was not found, it will return an empty string instead of undefined (which would be set to the input's value and shown as the undefined string).

jQuery

$('#Search').val(function() {
    return (window.location.search.match(/(?:^|[&;])Search=([^&;]+)/) || [])[1];
});

jsFiddle.

The (/* get GET param */ || []) is so the code will not error if the GET param was not found.

2 Comments

I don't know why it didn't work: $('#search_box').val(function() { return (window.location.searchresults.match(/(?:^|[&;])Search=([^&;]+)/) || [])[1]; }); the div's id is really search_box and the page's url is actually searchresults.asp. I modified the code accordingly, but it didn't work. Do you know why?
@user your code is wrong. Why did you put searchresults in there?

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.