Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-11-2009, 09:54 PM   PM User | #1
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 138
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
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
likon is offline   Reply With Quote
Old 11-11-2009, 10:14 PM   PM User | #2
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,005
Thanks: 65
Thanked 20 Times in 20 Posts
BubikolRamios is on a distinguished road
You cant.

Only way is to have iframes, but that is not what you want.
BubikolRamios is offline   Reply With Quote
Old 11-11-2009, 11:49 PM   PM User | #3
Old Pedant
Master Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
Old Pedant has a spectacular aura aboutOld Pedant has a spectacular aura about
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."
Old Pedant is offline   Reply With Quote
Old 11-11-2009, 11:54 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
rnd me will become famous soon enoughrnd me will become famous soon enough
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.
__________________
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
rnd me is offline   Reply With Quote
The Following 2 Users Say Thank You to rnd me For This Useful Post:
BubikolRamios (11-12-2009), Old Pedant (11-12-2009)
Old 11-11-2009, 11:57 PM   PM User | #5
Old Pedant
Master Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
Old Pedant has a spectacular aura aboutOld Pedant has a spectacular aura about
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."
Old Pedant is offline   Reply With Quote
Old 11-11-2009, 11:59 PM   PM User | #6
Old Pedant
Master Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
Old Pedant has a spectacular aura aboutOld Pedant has a spectacular aura about
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."
Old Pedant is offline   Reply With Quote
Old 11-12-2009, 12:26 AM   PM User | #7
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 138
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
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
likon is offline   Reply With Quote
Old 11-12-2009, 12:47 AM   PM User | #8
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 138
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
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
likon is offline   Reply With Quote
Old 11-12-2009, 01:19 AM   PM User | #9
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
rnd me will become famous soon enoughrnd me will become famous soon enough
Quote:
Originally Posted by likon View 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
that script is terrible by the way.
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..
rnd me is offline   Reply With Quote
Old 11-12-2009, 01:27 AM   PM User | #10
Old Pedant
Master Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
Old Pedant has a spectacular aura aboutOld Pedant has a spectacular aura about
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 problems it had: (1) if you had an item in the querystring with no = and no value, it would have choked on nameVal[1]. (2) If you had any values that needed to be escaped in the query string (e.g., a space or most any special character) then his code would not decode the value for you.

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."
Old Pedant is offline   Reply With Quote
Old 11-12-2009, 01:32 AM   PM User | #11
Old Pedant
Master Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
Old Pedant has a spectacular aura aboutOld Pedant has a spectacular aura about
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."
Old Pedant is offline   Reply With Quote
Old 11-12-2009, 02:24 AM   PM User | #12
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
rnd me will become famous soon enoughrnd me will become famous soon enough
Quote:
Originally Posted by Old Pedant View Post
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]) : "";
     }
}
what if try to pass "http://www.codingforums.com/showthread.php?t=181928" as the value of an "oldUrl" param with the above code?

decodeURIComponent helps, but a true parser is the only way to ensure that all values can be obtained...
__________________
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
rnd me is offline   Reply With Quote
Old 11-12-2009, 02:28 AM   PM User | #13
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 1,563
Thanks: 3
Thanked 214 Times in 202 Posts
rnd me will become famous soon enoughrnd me will become famous soon enough
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);
you can un-functionize it if needed, but it's nice to have it as a function so you can parse hrefs and the like.
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..
rnd me is offline   Reply With Quote
Old 11-12-2009, 05:33 AM   PM User | #14
Old Pedant
Master Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 5,631
Thanks: 20
Thanked 882 Times in 872 Posts
Old Pedant has a spectacular aura aboutOld Pedant has a spectacular aura about
*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>
The browser will always do the right thing, and escape() is quite up to the task.

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."
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:01 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.