【发布时间】:2013-11-10 08:01:32
【问题描述】:
我有一个类似/accounts/?status=done的网址
所以在加载文档时,我想获得 current url 和 GET data 就像 status=done 一样
所以下面是我的代码,它可以获取当前的 url,但不能获取数据
<script>
$(window).load(function() {
//alert("window load occurred!");
var pathname = window.location.pathname;
console.log(pathname);
$.get(pathname,function(data){ console.log(data) });
});
</script>
那么如何在加载页面时在 jquery 中获取当前 url 和 GET 数据,例如 ?status=done ?
编辑
所以我根据下面提供的链接进行了编辑,但仍然无法打印查询字符串参数
<script>
function getParameterByName(name) {
console.log(name+"......................");
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(window).load(function() {
//alert("window load occurred!");
var pathname = window.location.pathname;
console.log(pathname+"hurayyyyy");
console.log(pathname);
var res = getParameterByName(window.location.pathname)
console.log(res+"oooppppppppppppssssssssssss");
});
</script>
结果如下图
【问题讨论】:
-
我添加了一个编辑,请移动让我朝着正确的方向前进..
标签: jquery url get jquery-load