How to use JavaScript to obtain address bar parameters _ javascript skills

Source: Internet
Author: User
This article mainly introduces several methods and precautions for obtaining address bar parameters using JavaScript, which are very practical and recommended to you. The code is as follows:


/**
* Get the address bar parameters
*
* @ Example GetUrlString ('id ')
*
* @ Desc add a judgment when calling to ensure the program will not go wrong
* Var myurl = GetUrlString ('id ');
* If (myurl! = Null & myurl. toString (). length> 1 ){
* Alert (GetUrlString ("id "));
*}
*
* @ Param String param: obtain the parameter name in the address bar.
* @ Return String Value
* @ Type String
*
* @ Name GetUrlString ()
*
*/
Function GetUrlString (param ){
Var sValue = location. search. match (new RegExp ("[\? \ &] "+ M +" = ([^ \ &] *) (\ &?) "," I "));
Return sValue? DecodeURI (sValue [1]): decodeURI (sValue );
}

Make such a unified judgment during the call to avoid passing parameters. for example, if your address is abc.html and there is no parameter, then the strong line outputs the call result and sometimes reports an error.

The code is as follows:


Window. onload = function (){
Var myurl = GetParm ("id ");
If (myurl! = Null & myurl. toString (). length> 1 ){
Alert (GetParm ("id "));
}
}

In this way, no error is reported!

Note: ECMAScript v3 has removed the unescape () function from the standard and is opposed to using it. Therefore, use decodeURI () and decodeURIComponent () instead.

Have you understood how to use JavaScript to obtain the address bar parameters? if you have any questions, leave a message.

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.