Posted in Code Snippets, Javascript

Get query string value

Get the value of a field from query string using JavaScript:

function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)
                              (&|#|;|$)').exec(location.search)||[,""])
                              [1].replace(/\+/g, '%20'))||null
}

When your URL is something like this:

https://www.domain.com/getQueryString.html?author=james

Call the function like this:

var author = getURLParameter('author');

now variable author has james in it.

Advertisements

2 thoughts on “Get query string value

  1. I’ll immediately grab your rss as I can’t to find your e-mail subscription link or e-newsletter service.
    Do you’ve any? Kindly let me know in order that I may just subscribe.
    Thanks.

    Liked by 3 people

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s