The method of obtaining parameters in the js address bar can solve the problem of Chinese garbled characters and can support Chinese parameters

Reprinted: https://www.cnblogs.com/jorzen1984/p/6632918.html

The method of obtaining parameters in the js address bar solves the problem of Chinese garbled characters and can support Chinese parameters.
Without further ado, just upload the code directly. There are two types in total.

The first one: the parameters can only be in English.

//Get address bar parameters//Only English parameters
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + “=([^&]*)(&|$)” ); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }



The second type: the parameter can be Chinese or English

// Get address bar parameters // Can be Chinese parameters
function getUrlParam(key) { // Get parameters var url = window.location.search; // Regular filter address bar var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)"); // match target parameter var result = url.substr(1).match(reg); // return parameter value return result ? decodeURIComponent( result[2]) : null; }








Guess you like

Origin blog.csdn.net/weixin_42692989/article/details/90721971