Monday 18 February 2013

How to get query string using java script

In the World Wide Web, a query string is the part of a uniform resource locator (URL) that contains data to be passed to web applications.Using query string user can pass the some data with URL that helps to take some action.I am representing an example which help you to find data with URL


Passing query string<a href="demo.htm?item1">Item </a>



Getting Query String

function getstring()
{
         var ob=window.location.href.substr(window.location.href.indexOf("?")+1);

}

Using Regular expression

function getQueryStringValue(key)
{
    return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));

}

Calling : 

getQueryStringValue('id');

No comments:

Post a Comment