林不知道如何實際訪問中職變量網址
首先,如果他們在URL,他們GET
變量,而不是POST
變量(這是在請求正文中發送給服務器)。具體而言,它們是查詢字符串上的值。
您可以通過location.search
訪問查詢字符串。不幸的是,它出現在地址欄中,而不是很好地解析出來,但有一些插件可以用來解析它,like this one可以讓你訪問你的pg2
這個變量:
var pg2 = $.url(location).param('pg2');
這pg2
變量將是undefined
如果有查詢字符串沒有匹配的參數。
這只是一個例子,有幾個查詢字符串/ URL解析插件可用,或者當然你可以推出自己的。這裏有一個我做了幾年前:
/**
* Split up the given string (for instance, window.location.search) into component parts.
*
* @param str The string
* @param The component parts as keys on an object; if the query string has repeated entries,
* that key's value will be an array; a key with no value is present with the value
* `undefined`.
*/
$.splitQueryString = splitQueryString;
function splitQueryString(str) {
var entries, parts, result, entryIndex, key, newVal, oldVal;
// We return the result as an object
result = {};
// Skip a leading ? if any
if (str.charAt(0) === '?') {
str = str.substring(1);
}
// Strip anything after the hash symbol
index = str.indexOf('#');
if (index >= 0) {
str = str.substring(0, index);
}
// decodeURIComponent won't do '+' => ' ', so do it
str = str.replace(/\+/g, ' ');
// Split into entries
entries = str.split('&');
for (index = 0; index < entries.length; ++index) {
parts = entries[index].split('=');
key = decodeURIComponent(parts[0]);
newVal = parts[1];
if (typeof newVal !== 'undefined') {
newVal = decodeURIComponent(newVal);
}
if (key in result) {
oldVal = result[key];
if ($.isArray(oldVal)) {
oldVal.push(newVal);
}
else {
result[key] = [oldVal, newVal];
}
}
else {
result[key] = newVal;
}
}
// Done
return result;
}
用法:
var pg2 = $.splitQueryString(location.search).pg2;
的[在JavaScript中獲取查詢字符串值]可能重複(http://stackoverflow.com/questions/901115/get -query-string-values-in-javascript) – 2012-03-28 10:55:25