Home  >  Article  >  Web Front-end  >  How to get address bar parameters using JavaScript_javascript skills

How to get address bar parameters using JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:25:281258browse

Copy code The code is as follows:

/**
* Get address bar parameters
*
* @example GetUrlString('id')
*
* @desc Add judgment when calling to ensure that the program will not go wrong
* var myurl = GetUrlString('id');
* if (myurl != null && myurl.toString().length > 1) {
*                    alert(GetUrlString("id"));
*           }
*
* @param String param To get 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 this judgment uniformly when calling to avoid that if you do not pass parameters, for example, your address is abc.html and there are no parameters behind it, then forcibly output the call result and sometimes an error will be reported

Copy code The code is as follows:

window.onload = function() {
var myurl = GetParm("id");
If (myurl != null && myurl.toString().length > 1) {
alert(GetParm("id"));
}
}

This way there will be no errors!

Note: ECMAScript v3 has removed the unescape() function from the standard and deprecated its use, so it should be replaced by decodeURI() and decodeURIComponent().

Have you guys understood how to use JavaScript to get the address bar parameters? If you have any questions, please leave a message.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn