URL Get parameter value

Source: Internet
Author: User

How to get the Address bar parameters using JS (super Simple)

Method One: Use regular expressions to get the address bar parameters: (highly recommended, both practical and convenient!) )

function getquerystring (name) {varReg =NewREGEXP ("(^|&)"+ name +"= ([^&]*) (&|$)"); varR = Window.location.search.substr (1). Match (REG); if(r!=NULL)returnUnescape (r[2]);return NULL;} //Calling MethodsAlert (GetQueryString ("Parameter name 1"); alert (GetQueryString ("Parameter Name 2"); alert (GetQueryString ("Parameter name 3"));

Here's an example:

If the Address bar URL is: abc.html?id=123&url=http://www.maidq.com

So, but you use the above method to invoke: Alert (getquerystring ("url"));

A dialog box pops up: the content is http://www.maidq.com

If using: Alert (getquerystring ("id")), then the popup content is 123;

Of course, if you do not pass parameters, such as your address is abc.html back no parameters, the force output call results sometimes error:

So we have to add a judgment to determine if the argument we requested is empty, first assign the value to a variable:

var myurl=getquerystring ("url"); if (Myurl! =null && myurl.tostring () .length>1) {   alert (getquerystring (  "URL"));

This will not be an error!

URL Get parameter value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.