1

I wanna to get the query string of a page within a JavaScript block. for example:

The page has a URL like localhost:1234/Test/page1.aspx?ID=10

How can I get '10' with JavaScript?

1 Answer 1

3

window.location.search is the way to go.

Here's an example function:

function getQSVar( varname ) { 
   var query = window.location.search.substring( 1 ); 
   var vars = query.split( "&" );
   var len = vars.length; 
   for ( var i = 0; i < len; i++ ) { 
      var pair = vars[ i ].split( "=" ); 
      if ( pair[ 0 ] == varname ) { 
         return pair[ 1 ]; 
      } 
   } 
   return null;
}

Usage:

var IDValue = getQSVar( 'ID' ); // 10
Sign up to request clarification or add additional context in comments.

2 Comments

You might want to split on /[&;]/ instead.
@Jacob Relkin How do I pass it to a textbox on an aspx page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.