views:

28

answers:

1

I have the following:

window.linkFrom;

$(document).ready(function(){

    $("a.linker").click(function(event){

            window.linkFrom = $(this).closest("div").attr("id");

            alert(window.linkFrom); 

            });

 });

and want to pass the var linkFrom to a different script on a second page:

window.linkFrom;

(document).ready(function(){    

            alert(window.linkFrom);

});

How do I make this work?

TIA.

+2  A: 

The script environments of two different pages are entirely independent, so you cannot communicate over variables like this. Basically you have two choices:

  1. Use cookies.
  2. Modify the calls to the second page to add e.g. #linkFrom=asdf to the address and parse that in the js of the second page.

Clarifying edit: scripts -> script environments

clacke
Thanks Clacke. What if I were to use the first script on both pages. Would the variable save from page to page?
circey
No, it doesn't matter. From one page load to another the scripting environment is reset, so you need to transfer the information through a side channel, like cookies or http fragments (the right-hand side of the `#`).
clacke