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

0
exalkoniumCommented:
You could then access them on your next page inside an associative array:
0
hieloCommented:
<script>
window.onload=function(){
document.getElementById('hiddenField').value=location.pathname;
}
</script>
<form>
<input type="hidden" name="url" id="hiddenField" value="" />
</form>
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

exalkoniumCommented:
And it wouldn't be considered XSS as long as both pages exist on the same domain.
0
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

0
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

0
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!
0
exalkoniumCommented:
Ah, you are correct. Sorry, like I said, not my day...seem to be forgetting a lot.
0
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

0

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. :)
0
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.