PDA

View Full Version : Passing var from page to page



stalkio
04-16-2010, 03:30 AM
Not sure if this should be posted in here or on the html board, if it's in the wrong place let me know and I will move.

I'm trying to make a site where you can select your background for a page by clicking on a button. This is working.

However when you move to a new page it does not pass the style background image across. Does anyone have any ideas on how I might achieve this?

Can I set a variable that I can access from multiple pages?

Any info would be great.

Ryan

djr33
04-16-2010, 03:46 AM
There are only two ways that Javascript can pass a variable to the next page:
1. Send it directly as part of the request, either as a variable in a submitted form or attached to the URL:
page.htm?var=value
(Then on the next page you can get that).
That's reliable, but very hard to get setup.

The easier way:
2. Use cookies.
Cookies are unreliable over a long period of time (they probably will be there on a return visit within a day or two, but maybe not), but they are reliable for a single visit to the site. And since this isn't crucial anyway, that's probably ok. Note, however, that this requires cookies are enabled. For a few visitors, they may be disabled for security. It's probably not something to worry about, though. This is the closest to a cross-page "variable" you'll get in Javascript.



Of course the reliable way that doesn't rely on the user's computer to have certain things enabled is to instead use a server side language like PHP to set and store the value. Using sessions within PHP (or cookies if you'd prefer) you can do the same thing and not worry about Javascript being enabled. The methods above won't work if Javascript is not available which does happen sometimes (more than cookies being disabled, I think).



Given all of this, the short answer to your question is to use cookies. If that doesn't sound like a good solution, let us know what would be. And if you have PHP on your server (or ASP, CGI, etc.) then you may want to think about using that.

stalkio
04-16-2010, 03:59 AM
Thanks for the swift reply, I will go and google the hell out the cookie solution as that sound pretty much perfect.

This is for a .mobi project (just cutting my teeth) so maybe cookies don't work the same on mobile platforms? I feel a lot of research coming on :eek:

Might be that it's just a bridge to far at the moment.

Cheers for the help

Stalks

djr33
04-16-2010, 05:40 AM
On primative systems cookies may not work. But cookies have been standard for something like at least 10 years, so I wouldn't worry about it too much. the iphone, for example, fully supports them. Basically if you can log into a website then it's very likely that you can handle cookies, and most phones (even many simpler ones) can do this. More phones will be moving toward the iphone's state of using a real browser-- the iphone runs 'real' safari more or less. (Phones don't allow plugins like flash or Java applets [javascript is ok though].)
In my use of the iphone, the only problem I've had is accessibility of dynamic menus where, for example, you must hover then click-- the iphone doesn't really have this type of input, just click.

Regardless, you won't find a much better solution unless you can do it serverside-- it many ways that's a lot simpler, but it requires the more advanced step of basing [parts of] your website in PHP [or ASP, CGI, etc].