jQuery URL GET变量

你奥古斯丁和

我有一种GET在jQuery URL中使用变量的可能方法像在PHP中一样,我们有类似以下内容:

header('location : path/to/page.php?id='.$id);

在page.php中,我们这样做:

$id = $_GET['id'];

因此,在jQuery中我们可以做类似的事情:

window.location.replace(path/to/page.html/* and pass the url query here*/);
phreakv6

我认为您正在寻找的是在javascript中访问查询字符串(php中的$ _GET)。您可以为此使用此功能。

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

然后调用getParameterByName('id')以获取?id=val当前URL的一部分。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章