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.