获取html url var

时间:2014-03-07 10:26:23

标签: javascript jquery

我正在使用下面的代码将变量发送到另一个页面,但我得到了#34; undefined"

<script type="text/javascript">
$(document).ready(function() {

$('.letterz').click(function () {
var selectedletter = $(this).attr("id");
$("#celebdetails").load("reveal.html?surname="+selectedletter, function() {
$('.myloader').fadeOut("fast");
$('#celebdetails').fadeIn("slow");
});
});

});
</script>

我的HTML

<div class="myloader"></div>

<div id="#celebdetails" style="display:none;"></div>

<div class="letterz" id="A"></div>
<div class="letterz" id="B"></div>
<div class="letterz" id="C"></div>
<div class="letterz" id="D"></div>

在我发送参数姓氏的第二页中,我使用下面的代码来获取它:

<script type="text/javascript">
$(document).ready(function() {

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

var me = getUrlVars()["surname"];   
alert(me);

});
</script>

提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个:

function geturlvar() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    vars[key] = value;
});
return vars;
 }

称之为:

 var as = geturlvar()['urlvar'];

所以网址:http://stackoverflow.com?result=202

然后:var myvar = geturlvar()['result'];

myvar contains : 202;