如何简单地通过JQuery 获取GET和POST值?
我想做的是这样的:
$('#container-1 > ul').tabs().tabs('select', $_GET('selectedTabIndex'));
如何简单地通过JQuery 获取GET和POST值?
我想做的是这样的:
$('#container-1 > ul').tabs().tabs('select', $_GET('selectedTabIndex'));
Answers:
对于GET参数,您可以从中获取它们document.location.search:
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
$_GET[decode(arguments[1])] = decode(arguments[2]);
});
document.write($_GET["test"]);
对于POST参数,您可以将$_POSTJSON格式的对象序列化为<script>标记:
<script type="text/javascript">
var $_POST = <?php echo json_encode($_POST); ?>;
document.write($_POST["test"]);
</script>
在进行此操作(在服务器端执行操作)时,也可以在PHP上收集GET参数:
var $_GET = <?php echo json_encode($_GET); ?>;
注意:您需要PHP 5或更高版本才能使用内置json_encode功能。
更新:这是一个更通用的实现:
function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
var $_GET = getQueryParams(document.location.search);
jQuery有一个插件可以获取名为.getUrlParams的 GET参数。
对于POST,唯一的解决方案是使用PHP将POST回显到javascript变量中,如Moran建议的那样。
为什么不使用旧的PHP?例如,假设我们收到一个GET参数'target':
function getTarget() {
var targetParam = "<?php echo $_GET['target']; ?>";
//alert(targetParam);
}
或者,您可以使用一个http://plugins.jquery.com/project/parseQuery,它小于大多数(最小449字节),返回一个表示名称-值对的对象。
您可以尝试使用jQuery的查询字符串对象插件。
这是将所有GET变量收集到全局对象中的一种方法,这是经过几年优化的例程。由于jQuery的兴起,现在似乎将它们存储在jQuery本身中是合适的,我正在与John一起检查潜在的核心实现。
jQuery.extend({
'Q' : window.location.search.length <= 1 ? {}
: function(a){
var i = a.length,
r = /%25/g, // Ensure '%' is properly represented
h = {}; // (Safari auto-encodes '%', Firefox 1.5 does not)
while(i--) {
var p = a[i].split('=');
h[ p[0] ] = r.test( p[1] ) ? decodeURIComponent( p[1] ) : p[1];
}
return h;
}(window.location.search.substr(1).split('&'))
});
用法示例:
switch ($.Q.event) {
case 'new' :
// http://www.site.com/?event=new
$('#NewItemButton').trigger('click');
break;
default :
}
希望这可以帮助。;)
jQuery插件看起来不错,但是我需要一个快速的js函数来解析get参数。这是我所发现的。
如果您的$ _GET是多维的,则可能是您想要的:
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
//handling for multidimensional arrays
if(decode(arguments[1]).indexOf("[]") > 0){
var newName = decode(arguments[1]).substring(0, decode(arguments[1]).length - 2);
if(typeof $_GET[newName] == 'undefined'){
$_GET[newName] = new Array();
}
$_GET[newName].push(decode(arguments[2]));
}else{
$_GET[decode(arguments[1])] = decode(arguments[2]);
}
});
简单,但对从URL获取变量/值有用:
function getUrlVars() {
var vars = [], hash, hashes = null;
if (window.location.href.indexOf("?") && window.location.href.indexOf("&")) {
hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
} else if (window.location.href.indexOf("?")) {
hashes = window.location.href.slice(window.location.href.indexOf('?') + 1);
}
if (hashes != null) {
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
}
return vars;
}
我在互联网上的某个地方找到了它,只修复了一些错误
使用以下功能:
var splitUrl = function() {
var vars = [], hash;
var url = document.URL.split('?')[0];
var p = document.URL.split('?')[1];
if(p != undefined){
p = p.split('&');
for(var i = 0; i < p.length; i++){
hash = p[i].split('=');
vars.push(hash[1]);
vars[hash[0]] = hash[1];
}
}
vars['url'] = url;
return vars;
};
和访问变量vars['index']这里'index'是GET变量的名称。
http://foo.com/?a=b#bar。它认为b#bar是的价值a。
我的方法:
var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();