地址 http://localhost:8080/uploadFile.html?contractNo="2018-塑-崇-0089"
方法一:构造一个含有目标参的的正则表达式对象
/**
* 获取URL参数并解码返回
* @param name
* @returns
*/
function getUrlParam2(name){
//构造一个含有 --(目标参数)--的正则表达式对象
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
//匹配目标参数
var r = window.location.search.substr(1).match(reg);
//返回参数值
if (r!=null) return decodeURI(r[2]);
return null;
}
//调用
var contractNo = getUrlParam2("contractNo");
alert(contractNo);
方法二:
JS 的方法 encodeURI 和 encodeURIComponent
它们都是编码URL,唯一区别就是编码的字符范围
encodeURI方法:不会对下列字符编码
ASCII字母、数字、~!@#$&*()=:/,;?+'
encodeURIComponent方法:
不会对下列字符编码
ASCII字母、数字、~!*()'
所以encodeURIComponent比encodeURI编码的范围更大。
举例子来说,encodeURIComponent会把 http:// 编码成 http%3A%2F%2F 而encodeURI却不会。
使用场景:
如果你需要编码整个URL,那么用encodeURI。
如果你需要编码URL中的参数的时候,那么使用encodeURIComponent是最好方法。