Wednesday 28 June 2017

Parse URL with jquery/ javascript?

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


You can use a trick of creating an a-element, add the url to it, and then use its 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

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive