Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I parse an url with jquery/ javascript?

For instance I have this in my string,

http://mysite.com/form_image_edit.php?img_id=33

I want to get the value of img_id

I know I can do this easily with php with parse_url(), but I want to know if it is possible with javascrip.

share|improve this question
2  
With php you dont need to use the parse_url() function to get the img_id, just use $_GET['img_id] – mdaguerre Jul 11 '11 at 0:28
1  
possible duplicate of Get query string values in JavaScript – alex Jul 11 '11 at 0:51

6 Answers

up vote 44 down vote accepted

You can use a trick of creating an a-element, add the url to it, and then use it's Location object.

function parseUrl( url ) {
    var a = document.createElement('a');
    a.href = url;
    return a;
}

parseUrl('http://mysite.com/form_image_edit.php?img_id=33').search

Which will output: ?img_id=33


You could also use php.js to get the parse_url function in JavaScript.


Update (2012-07-05)

I would recommend using the excellent URI.js library if you need to do anything more than super simple URL handling.

share|improve this answer
+1 was just about to post this :) – alex Jul 11 '11 at 0:50
1  
Which is equivalent to url.match(/\?.+/)[0], which is somewhat shorter and likely a lot faster. – RobG Jul 11 '11 at 6:09
3  
wow, never knew about this trick, it made my day after sorting through all the worlds ugliest regular expressions =) – Greg Guida Jul 28 '11 at 18:34
1  
@Christophe No it shows you what properties are available. When you assign an url to the href property of an anchor element, it get's all the properties of the Location object. – Sindre Sorhus May 31 '12 at 21:30
1  
@SindreSorhus Amazing! Could you share a reference that explains it and details browser support? Is this also true for elements that have a src attribute (link, iframe)? – Christophe May 31 '12 at 22:19
show 4 more comments

If your string is called s then

var id = s.match(/img_id=([^&]+)/)[1]

will give it to you.

share|improve this answer

Try this:

    var url = window.location;
    var urlAux = url.split('=');
    var img_id = urlAux[1]
share|improve this answer

got it from google, try to use this method

function getQuerystring2(key, default_) 
{ 
    if (default_==null) 
    { 
        default_=""; 
    } 
    var search = unescape(location.search); 
    if (search == "") 
    { 
        return default_; 
    } 
    search = search.substr(1); 
    var params = search.split("&"); 
    for (var i = 0; i < params.length; i++) 
    { 
        var pairs = params[i].split("="); 
        if(pairs[0] == key) 
        { 
            return pairs[1]; 
        } 
    } 


return default_; 
}
share|improve this answer

Existing good jQuery plugin URL Parser

share|improve this answer

You can use my URL.js library.

url.parse("http://mysite.com/form_image_edit.php?img_id=33").get.image_id === "33"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.