0

I found one library, jQuery URL Parser, which seems to be full-featured as far as parsing out the various segments of a URL. I'm trying to go one step further though, and parse the values of the query parameters.

For example, given the URL "http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=rm+-rf+%2F" I'd like to get "rm -rf /" for the q parameter. Any suggestions would be greatly appreciated.

As a side note, I'm not operating on the URL of the current page so any location magic doesn't apply.

2 Answers 2

1

It looks as though the library you referenced can indeed retrieve query string values as given under the section called Query string parameters:

The .param() method is used to return the values of querystring parameters.

Pass in a string to access that parameter's value:

$.url('http://allmarkedup.com?sky=blue&grass=green').param('sky'); // returns 'blue' If no argument is passed in it will return an object literal containing a key:value map of all the querystring parameters.

$.url('http://allmarkedup.com?sky=blue&grass=green').param(); // returns { 'sky':'blue', 'grass':'green' } Note that the .param() method will work on both ampersand-split and semicolon-split querystrings.

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

2 Comments

This probably should be used together with decodeURIComponent to achieve what the OP wants..
The combination of mellamokb's answer, Shadow Wizard's comment and replacing +s with spaces worked worked perfectly. Thanks!
0

you can use your own small lightweight code to do just that :

    function getParam(url, param) {
        if (!url.indexOf('?')) {
            return 'undefined';
        }
        url = url.split('?')[1];
        var arr = url.split('&');
        for (var i = 0; i < arr.length; i++) {
            var key = arr[i].split('=')[0], val = arr[i].split('=')[1];
            if (key === param) {
                return val;
            }
        }
        return 'undefined';
    }

Comments

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.