Get URL Parameters with JavaScript

With the wide use of AJAX, sometimes we need get values of parameters from current URL, the below function is an very handy way to do so.

function getQuery(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}

Example:
Get the id’s value from http://codemug.com/a.php?c=my&id=7&m=1

getQuery(“id”)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>