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

5
  • What gives you: console.log(vars); ??? Commented Mar 7, 2014 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 Commented Mar 7, 2014 at 10:33
  • @A.Wolff gives me: Uncaught ReferenceError: vars is not defined Commented Mar 7, 2014 at 10:34
  • alot of post in stackoverflow got the answer. this one stackoverflow.com/questions/901115/… Commented Mar 7, 2014 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 Commented Mar 7, 2014 at 10:35

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

i am still getting undefined... :-(
So check your URL is it contain your passed variable ? @lrene
if i put an alert when i am pressing a letter it appears the id.. that means that clickable div with id gets the id var.. the second page doesn't grab it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.