Javascript window.location redirect and including previously defined variables

Sonny Forelli

Well-known member
May 8, 2008
2,330
89
48
Liberty City
Can anyone help me out with what I think is a .js sleep/setTimeout/pause type error?

I'm basically trying to grab variables from the url (name and email) and then include them into a 'linkout.php' redirect.

If I comment out the window.location..... line the variables show up (though directly against one another like: NameEmail ) on the page, however on the linkout.php page they're undefined.

If I manually define them (var name = "john") this passes correctly to the linkout.php page.

I'm thinking that perhaps this is a speed issue? Whereby it's redirecting so fast that it doesn't have time to grab them? If so can anyone help? Could setTimeout be used to delay by 1 second and remedy this? (not sure what this would look like).

Thanks!

Code:
var name = formData.display("name"); 
var email = formData.display("email");



window.location="linkout.php?name=" +name+ "&email=" +email;
 


never seen .display before, is that just echoing to the page? that would be causing all the errors you described if so, you're not actually setting any vars, you're just echoing the values out.

Get URL Variables - JavaScript - Snipplr Social Snippet Repository

you need to grab the get string off the url with something like that and assign to the window.location call.

Also, using timers in javascript to solve issues is a zero sum game, there are too many variables going on to assume you can accurately choose an appropriate delay setting.

Let me know if this works
 
sorry- here's the full code of what I'm doing.

This is a script I pulled from aweber on their page of how to grab variables to personalize a thank you page, and per their tutorial here: How Do I Display Subscribers' Names or Email Addresses On My Thank You Page? :: AWeber Knowledge Base

am then trying to define "email" and "meta_adtracking" <as you'll see in script right below>. I'm using meta_adtracking as my subid to track, and email as the users' email address.

All I need to have happen is for these two variables to be url appended to linkout.php. Why using .js at all? I need (want) to resize the window to full screen. If I remove this entirely everything works great via php, but somehow in the code above both email and metaadtracking are undefined.

But if I comment out the window.location....... line I see the email/meta_adtracking variables on the page fine. AND if I hardcode in john@john.com and 12345 these then pass to the linkout.php page.

So I'm stumped why I'm getting undefined- I put the stupid setTimeout (3000) in there thinking it was redirecting too fast but now this is still giving me an undefined error with the 3 second pause...

Thanks for your response!

Code:
<html>
<head>

<script type="text/javascript">

var formData = function() {

    var query_string = (location.search) ? ((location.search.indexOf('#') != -1) ? location.search.substring(1, location.search.indexOf('#')) : location.search.substring(1)) : '';
    var elements = [];

    if(query_string) {
       var pairs = query_string.split("&");
       for(i in pairs) {
          if (typeof pairs[i] == 'string') {
              var tmp = pairs[i].split("=");
              elements[unescape(tmp[0])] = unescape(tmp[1]);
          }
       }
    }

    return {
        display: function(key) {
            if(elements[key]) {
              document.write(elements[key]);
            } else {
              document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");
            }
        }   
    }



}();

</script>   
    
<script>

var email = formData.display("email"); 
var meta_adtracking = formData.display("meta_adtracking");

</script>

<SCRIPT>
<!--

function link()
{

window.location="linkout.php?email=" +email+ "&meta_adtracking=" +meta_adtracking;
}


function cont()
{
if (window.screen) self.resizeTo(screen.availWidth,screen.availHeight);



setTimeout('link()',3000);
}


// -->
</SCRIPT>
</head>

<body onload="javascript:cont()"> 




</body></html>
 
AH- can't assign, yeah that was gut worry was that something like that was happening and thus it couldn't 'pass' in the window.location line.

I'm reading that link now, don't think I understand it but at least i understand why it's not working.

Thanks
 
yeah, I found that aweber link while looking up the .display function

all that link I pasted in does is parse out the GET string from a url and separate the parts into a hash array. So if you have ?name=sonny&email=sonnyforelli@gmail.com....

in your js, you would do:


var hash = getUrlVars();
window.location="linkout.php?name=" +hash['name']+ "&email=" +hash['email'];
 
yeah, I found that aweber link while looking up the .display function

all that link I pasted in does is parse out the GET string from a url and separate the parts into a hash array. So if you have ?name=sonny&email=sonnyforelli@gmail.com....

in your js, you would do:


var hash = getUrlVars();
window.location="linkout.php?name=" +hash['name']+ "&email=" +hash['email'];


:bowdown::bowdown::bowdown:

Can't thank you enough- +REP!