1

i am using the code bellow to send a variable to another page but i got "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>

my 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>

And in the second page that i am sending the parameter surname i am using the code below to fetch it:

<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>

Thanks in advance

Irene T.
  • 1,393
  • 2
  • 20
  • 40
  • What gives you: `console.log(vars);` ??? – A. Wolff Mar 07 '14 at 10:31
  • I might be wrong but your getUrlVars function is called after it is loaded in the calling page (the first page) and that is why `getUrlVars()["surname"]` is undefined – Valentin D Mar 07 '14 at 10:33
  • @A.Wolff gives me: Uncaught ReferenceError: vars is not defined – Irene T. Mar 07 '14 at 10:34
  • alot of post in stackoverflow got the answer. this one http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript?rq=1 – wayne Mar 07 '14 at 10:34
  • @IreneT. but how you are checking for it? Put code just before returning it from function. BTW, why calling your variable as it is hash part of URL, that's not hash you are looking for, but that's just a misspelling – A. Wolff Mar 07 '14 at 10:35

1 Answers1

1

Try with This :

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

Call it like :

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

So URL : http://stackoverflow.com?result=202

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

myvar contains : 202;
ashbuilds
  • 1,401
  • 16
  • 33