jmoriarty
asked on
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!
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)
Thanks!
You could then access them on your next page inside an associative array:
<script>
window.onload=function(){
document.getElementById('h iddenField ').value=l ocation.pa thname;
}
</script>
<form>
<input type="hidden" name="url" id="hiddenField" value="" />
</form>
window.onload=function(){
document.getElementById('h
}
</script>
<form>
<input type="hidden" name="url" id="hiddenField" value="" />
</form>
And it wouldn't be considered XSS as long as both pages exist on the same domain.
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;
}
It's not my day. To add to my solution, to use vars:
vars = get_array();
alert(vars["pathname"]);//sample usage
ASKER
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!
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!
Ah, you are correct. Sorry, like I said, not my day...seem to be forgetting a lot.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did the trick. Thanks. :)
Open in new window