window.location.search的用法 和 地址欄的的javsscript編碼與解碼

ocation.search是從當前URL的?號開始的字符串 
如:http://www.51js.com/viewthread.php?tid=22720 javascript

它的search就是?tid=22720 php

 

eg:java

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 進行解碼。ui

實例

在本例中,咱們將使用 decodeURIComponent() 對編碼後的 URI 進行解碼:編碼

<script type="text/javascript">

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

document.write(+ "<br />")
document.write()

</script>encodeURIComponent(test1)decodeURIComponent(test1)

輸出:spa

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

 

 

 

參考:code

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

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

相關文章
相關標籤/搜索