window.location.search的用法 和 地址栏的的javsscript编码与解码

ocation.search是从当前URL的?号开始的字符串 
如:http://www.51js.com/viewthread.php?tid=22720 

它的search就是?tid=22720 

eg:

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r)
        return decodeURIComponent(r[2]);
}

JavaScript decodeURIComponent() 函数

定义和用法

decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。

实例

在本例中,我们将使用 decodeURIComponent() 对编码后的 URI 进行解码:

<script type="text/javascript">

var test1="http://www.w3school.com.cn/My first/"

document.write(encodeURIComponent(test1)+ "<br />")
document.write(decodeURIComponent(test1))

</script>

输出:

http%3A%2F%2Fwww.w3school.com.cn%2FMy%20first%2F
http://www.w3school.com.cn/My first/

参考:

http://shirlly.iteye.com/blog/385361

http://www.w3school.com.cn/jsref/jsref_decodeURIComponent.asp

原文地址:https://www.cnblogs.com/qqhfeng/p/10552533.html