The Location object contains information about the current URL.

The Location object is part of the Window object and is accessible through the window. Location property.

Correct encoding and decoding helps to process Chinese characters, while incorrect encoding will result in Chinese garbled characters.

1. Pass in a URL, parse the parameters, and return an object containing all the parameters:

function getParmas(url) { var temp1 = url.split('? '); var pram = temp1[1]; var keyValue = pram.split('&'); var obj = {}; for (var i = 0; i < keyValue.length; i++) { var item = keyValue[i].split('='); var key = item[0]; var value = item[1]; obj[key] = value; } return obj }Copy the code

2. Obtain the parameter value of the current page URL according to the parameter name:

Most of the online examples are decoded using unescape(), but the test Chinese is still garbled, to decodeURI() can be.

function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r ! = null) return decodeURI(r[2]); return null; }Copy the code

When used, it is best to add a null value judgment:

if (GetQueryString("sex")! =null&&GetQueryString("sex").length>0) { // }Copy the code