javascript: pass a variable from one page to another

Hey folks,

Is it possible to pass a javascript variable from one page to another? For example, on page1.htm I want to take the pathname (window.location.pathname) put it into a variable, and pass it to a hidden field contained on a form on page2.htm -- or would that be considered xss?

Thanks!
jmoriartyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

exalkoniumCommented:
You could do this through GET when you ask for page 2:
var pathname = encodeURIComponent(window.location.pathname);

window.location = "page2.htm?pathname=" + pathname; //or however you get to your next page

Open in new window

exalkoniumCommented:
You could then access them on your next page inside an associative array:
hieloCommented:
<script>
window.onload=function(){
document.getElementById('hiddenField').value=location.pathname;
}
</script>
<form>
<input type="hidden" name="url" id="hiddenField" value="" />
</form>
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

exalkoniumCommented:
And it wouldn't be considered XSS as long as both pages exist on the same domain.
exalkoniumCommented:
Comment #2 didn't post the code:
function get_array() {
  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;
}

Open in new window

exalkoniumCommented:
It's not my day. To add to my solution, to use vars:
vars = get_array();
alert(vars["pathname"]);//sample usage

Open in new window

jmoriartyAuthor Commented:
Hi exalkonium,

That's a great solution you've presented, and should work well, though I think we'll also need to access that vars["pathname"] with decodeURIComponent() to strip the encoding if I'm not mistaken. Going to test it out now.

Thanks to you and others!
exalkoniumCommented:
Ah, you are correct. Sorry, like I said, not my day...seem to be forgetting a lot.
exalkoniumCommented:
Let's try again (page2):
function get_array() {
  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]] = decoddeURIComponent(hash[1]);
  }
  return vars;
}
vars = get_array();
//vars["pathname"];

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmoriartyAuthor Commented:
That did the trick. Thanks. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.