這裏有很多很好的解決方案,但我想我會張貼自己的。 這裏有一個快速的小函數,我將它們放在一起,它將以window.location.search或者提供的搜索字符串值的格式解析查詢字符串;
它返回的值id對的哈希值,所以你能在形式引用它:
var values = getQueryParams();
values['id']
values['blah']
下面的代碼:
/*
This function assumes that the query string provided will
contain a ? character before the query string itself.
It will not work if the ? is not present.
In addition, sites which don't use ? to delimit the start of the query string
(ie. Google) won't work properly with this script.
*/
function getQueryParams(val) {
//Use the window.location.search if we don't have a val.
var query = val || window.location.search;
query = query.split('?')[1]
var pairs = query.split('&');
var retval = {};
var check = [];
for(var i = 0; i < pairs.length; i++) {
check = pairs[i].split('=');
retval[decodeURIComponent(check[0])] = decodeURIComponent(check[1]);
}
return retval;
}
您可以通過查詢字符串的值沒有字符串解析的URL你可以這樣做:
window.location.search.substr(1)
如果你想在頁面的名字之前?你仍然需要做一點字符串解析:
var path = window.location.pathname.replace(/^.*\/(.*)$/,'$1');
var query = path + window.location.search;
//If your URL is http://www.myserver.com/some/long/path/big_long%20file.php?some=file&equals=me
//you will get: big_long%20file.php?some=file&equals=me
希望這有助於! 乾杯。
你需要對它進行子串處理。如果您需要示例,請發表評論。 – 2009-07-23 13:53:42