你是混合location.hash
與location.search
- see here for the explanation
你可以只使用location.hash
並在你的「變量」之間加上一個分隔符:
例如..如果這是你的網址http://www.domainname.com/#changepassword-variable-another
th恩,你可以做
var hash = window.location.hash.substring(1);
var hasharray = hash.split['-']
if(hasharray[0] == 'changepassword') { // Password Change
alert('changepassword');
}
,或者您可以使用location.search
和下面的例子:
網址:http://www.domainname.com/?command=changepassword&var=variable&var2=another
此使用key=value
格式,然後用下面的方法來訪問values
function getQueryString() {
var result = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return result;
}
您可以通過傳遞函數key
var command = getQueryString()["command"];
if(command == 'changepassword') { // Password Change
alert('changepassword');
}
在你的代碼示例中有**沒有** jQuery ...這是計劃JavaScript – ManseUK 2012-03-24 07:41:30
,但具有'jQuery'標記將使範圍在jQuery中回答。而那些避開'javascript'標籤的'jQuery'人將會看到它突出顯示! – tusar 2012-03-24 07:50:03
@tusar我並沒有質疑標籤的需求(如果我認爲它不合適,我會編輯和刪除它) - 我只是陳述事實.... – ManseUK 2012-03-24 07:52:39