![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
Regular Coder ![]() Join Date: Apr 2007
Posts: 138
Thanks: 3
Thanked 1 Time in 1 Post
![]() |
passing javascript value from page to page
hello guys
the idea is to make "offline" bill of lading i used to do php thing and well this time i only need to kinda make bill of lading generator so i want to pass value from page 1 to page 2 and to page . all offline without web server interaction i was never fluent in javascript and i wanted to get a quick start from you guys how do i pass the form ( javascript variable from 1 page to another page ) i am googling this as well right now and hoping answer from codingforums thanks |
|
|
|
|
|
PM User | #3 |
|
Master Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
![]() ![]() |
Sure you can! Use cookies.
Okay, doesn't work if the user completely disables cookies. But works fine even if they only allow session cookies. ************ Alternative: Use the query string! Heck, that's probably the easiest way: Just use a <form method="get"> and the query string is automatically generated for you and then all you have to do is decode the string on the target page. Not hard, honest.
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
|
|
PM User | #4 |
|
Senior Coder ![]() Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
![]() ![]() |
window.name stays the same between reloads, and doesn't require cookies or altering urls...
you can even open a new window using window.open, and set its window.name with the open function function... it takes quite a bit less code than other solutions as well. |
|
|
|
| The Following 2 Users Say Thank You to rnd me For This Useful Post: | BubikolRamios (11-12-2009), Old Pedant (11-12-2009) |
|
|
PM User | #5 |
|
Master Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
![]() ![]() |
rndme: Are you suggesting that the user encode all the needed info as part of the window.name??
Wow. Cute idea!
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
|
|
PM User | #6 |
|
Master Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
![]() ![]() |
Though if you are going to use a <form> on the page, anyway, seems to me that just submitting the form with method=get (and then parsing the querystring in next page) has to be about the same work or less than gathering all the fields and creating an encode string in the window.name, come to think of it. Still, for only one or two values, I really like that window.name idea.
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
|
|
PM User | #7 |
|
Regular Coder ![]() Join Date: Apr 2007
Posts: 138
Thanks: 3
Thanked 1 Time in 1 Post
![]() |
hey guys can you give me a quick sample for that ?
i guess query string would work too bad I dont know javscript too much and I Have been using jquery for animation and stuff but i dont really know javascript and .. well the DOM element and stuff. can you give me a sample real quick ? thank you all |
|
|
|
|
|
PM User | #8 |
|
Regular Coder ![]() Join Date: Apr 2007
Posts: 138
Thanks: 3
Thanked 1 Time in 1 Post
![]() |
hello guys i found the easy way to do this in case other people are looking the same thing
http://javascript.internet.com/forms...me=dsa&age=dsa thank you for the help |
|
|
|
|
|
PM User | #9 | |
|
Senior Coder ![]() Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
![]() ![]() |
Quote:
for the simplest things, it will probably work, but it can't handle special chars or, for example, passing a full url from page to page. @Old Pedant: yes, that's exactly what i'm saying. window.name allows quite a bit more info to be passed than the queryString. in firefox 3.5 it's a LOT of data (never given it too much, don't know ceiling, but 50kb was fine). it's also faster, secure (never goes over the net), and it works in all browsers. In conjunction with JSON.stringify+eval, one can pass full strong-typed objects between pages with a single line of code. it's even possible, though a bit tricky in ie, to enable full, asynch communication between frames of different domains with window.name. The iframe catches top-set window.name when it loads, sets it's own window.name, and sets the frame's location back to it's refferer. Once the frame's domain matches the tops, the top can read the new window.name set by the frame, and begin the cycle anew. this capability has (sadly) been overlooked for years. I didn't come up with the trick, but i sure love it.
__________________
libs mini F ASPmini dnd OO(gen api) tmpl8 apps snippets blog Slides(demo maker) crypto image editor crapplets compressor prepage JSON(browser viewer) time notepad bench Last edited by rnd me; 11-12-2009 at 01:27 AM.. |
|
|
|
|
|
|
PM User | #10 |
|
Master Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
![]() ![]() |
Ugh. That code has a couple of problems. And an inefficiency.
Let's rewrite it. For starters, why a function when you want it to happen all the time??? Code:
var params = [];
if ( location.search.length > 1 )
{
var pairs = location.search.substring(1).split("&");
for (var i=0; i<pairs.length; i++)
{
nameVal = pairs[i].split('=');
params[nameVal[0]] = ( nameVal.length > 1 ) ? unescape(nameVal[1]) : "";
}
}
The efficiency issue: He was mucking with the entire document.URL instead of *just* looking at the query string (which is in location.search).
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
|
|
PM User | #11 |
|
Master Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
![]() ![]() |
I'd go with window.name for only a few form fields; assign them to members of an object and then use JSON as rndme said. But for many form fields, what the heck: Let the browser do the work for you. As you can see, the code to get the parameters is only 6 JS code statements (and we could write it in fewer if we wanted obscure code <grin/>).
Exception: If you didn't want the info to be sent across the net, then of course window.name is the only good answer.
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
|
|
PM User | #12 | |
|
Senior Coder ![]() Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
![]() ![]() |
Quote:
decodeURIComponent helps, but a true parser is the only way to ensure that all values can be obtained... |
|
|
|
|
|
|
PM User | #13 |
|
Senior Coder ![]() Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
![]() ![]() |
a queryString parser that satisfies wiseguys:
Code:
// turn URL'QS into an object using a parser. takes full urls...
function parseQS(str){
var ob={}, float="", key="", dc=decodeURIComponent;
for( var i=0, mx=str.length; i<mx;i++){
var it=str[i];
if(it==="="){ key=float; float=""; continue;}
if(!it.search(/^[?&]/)){
if(it==="&" && str.slice(i+1,i+5)==="amp;"){ i=(i+4);float+="&"; continue;}
if(key){ob[key]=dc(float);} key=""; float=""; continue;
}
float+=it;
}
ob[key]=dc(float);
return ob;
}
//usage:
parseQS(location.href);
This one won't be fooled by missing values, extra "?"s, XML encoded ampersands, or any of the others pitfalls commonly seen in 90% of the examples floating around... hech, you don't even have to chop-off http:// part. in fact don't: let the parse handle it for you...
__________________
libs mini F ASPmini dnd OO(gen api) tmpl8 apps snippets blog Slides(demo maker) crypto image editor crapplets compressor prepage JSON(browser viewer) time notepad bench Last edited by rnd me; 11-12-2009 at 02:41 AM.. |
|
|
|
|
|
PM User | #14 |
|
Master Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
![]() ![]() |
*IF* you allow people to create query strings "by hand," then yes, you need something like that.
But look what happens when you let the *browers* do it for you: Code:
<html>
<head>
<script>
var params = [];
if ( location.search.length > 1 )
{
var pairs = location.search.substring(1).split("&");
for (var i=0; i<pairs.length; i++)
{
nameVal = pairs[i].split('=');
params[nameVal[0]] = ( nameVal.length > 1 ) ? unescape(nameVal[1]) : "";
}
}
</script>
</head>
<body onload="document.getElementById('here').innerHTML = params['url'];">
url from form field from prior time on page:
<div id="here"></div>
<br/><hr/><br/>
<form>
url: <input name="url" size="80" value="http://www.codingforums.com/showthread.php?t=181928" />
<br/><br/>
<input type="submit" value="Try it!"/>
</form>
</body>
</html>
If this wasn't true, method=get and method=post wouldn't work right with PHP and ASP and JSP, among other server-side systems.
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|