-3

假设我有网页 1,它将我重定向到网页 2,但是像 xyz.com?a=qwerty 使用 JavaScript,我将如何检索 a 的数据?

4

3 回答 3

1

这是处理它的小功能

<script type="text/javascript">
function $_GET(q,s) { 
    s = s ? s : window.location.search; 
    var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i'); 
    return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined; 
} 
</script>

用法

<script type="text/javascript">
    alert('GET var myVar = '+$_GET('myVar'));
</script>
于 2013-03-27T18:30:10.047 回答
1

Javascript 作为一种语言没有直接的 URL 解析工具。但是,有许多库可供选择,它们提供这种类型的支持

于 2013-03-27T18:30:38.983 回答
0

你可以试试这样的脚本。

var str=window.location.href; var n=str.split("="); alert(n[1]);

此代码在 = 处拆分 URL,然后返回数据。

于 2013-03-27T18:29:41.827 回答