Ways to get Address bar parameters using JavaScript _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

/**
* Get Address bar parameters
*
* @example geturlstring (' id ')
*
* @desc call with the decision to ensure that the program is not wrong
* var myurl = geturlstring (' id ');
* IF (myurl!= null && myurl.tostring (). length > 1) {
* Alert (geturlstring ("id"));
*             }
*
* @param String param to get the name of the parameter 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);
}

Call the time unified to do such a judgment, to avoid you do not pass the parameters, such as your address is abc.html after no parameters, that forced output call results sometimes the error

Copy Code code as follows:

Window.onload = function () {
var myurl = getparm ("id");
if (Myurl!= null && myurl.tostring (). length > 1) {
Alert (Getparm ("id"));
}
}

So there won't be an error!

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

Do the small partners understand how to use JavaScript to get the address bar parameters, if you have any questions, leave a message.

Related Article

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.