kevglass
|
 |
«
Posted
2008-06-04 09:26:38 » |
|
I want to store my client state in cookies in the browser for Putty. There didn't seem to be a way to set cookies client side so I wrote a PHP script that the client calls the return from which sets the cookie, this isn't going to work in the future (some places I'd like to host can't support PHP). So, is there a way to set cookies client side without an external URL?
Kev
|
|
|
|
Riven
|
 |
«
Reply #1 - Posted
2008-06-04 09:50:46 » |
|
applet.showDocument("javascript:myFunction();"); Where myFunction() does what you want. --- this does NOT work in Opera --- it doesn't understand the 'javascript:' protocol A heck of a lot more stable is this: applet.showDocument("javascript:eval(hexToStr('"+strToHex("myFunction();")+"'));"); You have to implement strToHex and hexToStr yourself though. The workaround in Opera is to have this iframe in your page... applet.showDocument(" http://mysite.com/runscript.php?eval_hex="+strToHex("myFunction();"), "myIframeTarget"); where runscript.php reads the argument, decodes it with hexToStr and writes <script ...> </script> around it. I use this to communicate between a webpage and an applet (both ways) without LiveConnect (which is buggy in FF)
|
|
|
|
Matzon
|
 |
«
Reply #2 - Posted
2008-06-04 12:13:12 » |
|
what about using 1 2
| JSObject window = JSObject.getWindow(appletinstance); window.eval("myFunction();"); |
?
|
|
|
|
Games published by our own members! Check 'em out!
|
|
kevglass
|
 |
«
Reply #3 - Posted
2008-06-04 12:35:07 » |
|
Been meaning to ask about this JSObject stuff, is that compatible everywhere? Seemed to be borked on my IE.
Kev
|
|
|
|
|
|
kevglass
|
 |
«
Reply #5 - Posted
2008-06-04 14:07:40 » |
|
Yeah, went through that. The cookie management thing requires signed JARs - hence the PHP hack.
Kev
|
|
|
|
Markus_Persson
|
 |
«
Reply #6 - Posted
2008-06-04 14:17:29 » |
|
Isn't that just the two last examples (using CookieHandler)? The first two seems to do things manually via URLConnections.
|
|
|
|
kevglass
|
 |
«
Reply #7 - Posted
2008-06-04 15:05:26 » |
|
I can't see how the first two actually let you store and retrieve values as cookies though. The server has to be set up to set the cookie on the response doesn't it (which is where my PHP came in)?
Kev
|
|
|
|
Markus_Persson
|
 |
«
Reply #8 - Posted
2008-06-04 15:36:18 » |
|
Oh, I thought it was some kind of standard HTTP stuff. That's a shame.
Flash (and Silverlight) has some really nice limited local storage stuff that's even cross platform so you can get the same "cookies" on the same computer regardless of what browser you use. Java needs something like that.
|
|
|
|
kevglass
|
 |
«
Reply #9 - Posted
2008-06-04 15:53:06 » |
|
Yeah, I was kinda hoping the Java Preferences API was going to be it, but I seem to remember that was security manager restricted aswell. Kev
|
|
|
|
Games published by our own members! Check 'em out!
|
|
kevglass
|
 |
«
Reply #10 - Posted
2008-06-04 16:07:53 » |
|
Actually, if that JSObject is supported everywhere couldn't we use: 1 2 3 4 5 6
| JSObject document = (JSObject) window.getMember("document"); String load = (String) document.getMember("cookie");
document.setMember("cookie", "x=y; x2=y2"); |
Kev
|
|
|
|
Mr_Light
|
 |
«
Reply #11 - Posted
2008-06-04 16:16:50 » |
|
Actually, if that JSObject is supported everywhere couldn't we use:
It seems it's there since 1.3 http://java.sun.com/products/plugin/1.3/docs/jsobject.htmlThe problem isn't the java version, but the quality of the implementation might suck?
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
Riven
|
 |
«
Reply #12 - Posted
2008-06-04 19:19:58 » |
|
This might sound arrogant, but I don't understand why you'd look any further than my solution...
It works for me, in all browsers, even Safari.
|
|
|
|
brackeen
|
 |
«
Reply #13 - Posted
2008-06-04 19:22:20 » |
|
Watch out for the FF bug in 1.6.0_03 - 1.6.0_09 (fixed in update 10), where using LiveConnect causes problems: http://bugs.sun.com/view_bug.do?bug_id=6669818Web browsers may have the following limitations for cookies (according to RFC 2109 section 6.3): * 300 cookies total * 20 cookies per domain (per site, not per page) * 4,096 bytes per cookie (name and value combined) * Additionally, Internet Explorer allows only 4,096 bytes per domain (!) Also of course cookies can have limitations on the characters they can use. An easy way around that is to encode data as Base64. Since there are only 20 cookies per domain, what I do is only store one cookie per game, and that cookie is a Base64-encoded gzipped byte array of tons of data (name, previous scores, sound on/off, etc). In 6u10, the JNLP API can be used (PersistenceService).
|
|
|
|
|
kevglass
|
 |
«
Reply #14 - Posted
2008-06-04 20:07:19 » |
|
This might sound arrogant, but I don't understand why you'd look any further than my solution...
It works for me, in all browsers, even Safari.
Maybe I read it wrong - the first bit you said didn't work in Opera. The second bit (Opera workaround) relies on PHP (which isn't available on a site where I'm going to host - it also can't link out to somewhere that does use PHP). Thanks for the details brackeen - how do you go about setting cookies atm? Kev
|
|
|
|
Riven
|
 |
«
Reply #15 - Posted
2008-06-04 20:16:29 » |
|
Well, it *can* be implemented by PHP...
In javascript you can figure out your own URI using window.location.href (including the part after the '?') so you can put your decoder+eval(...) in your body.onLoad
Problem solved.
|
|
|
|
brackeen
|
 |
«
Reply #16 - Posted
2008-06-04 20:19:42 » |
|
Thanks for the details brackeen - how do you go about setting cookies atm?
Using JSObject (via reflection), but Riven's idea looks interesting for setting cookies... it would be a good workaround for the FF LiveConnect bug. Riven, how to you get cookies via this method?
|
|
|
|
|
kevglass
|
 |
«
Reply #17 - Posted
2008-06-04 20:27:52 » |
|
Cheers. So I could use that method in all cases?
Kev
|
|
|
|
Riven
|
 |
«
Reply #18 - Posted
2008-06-04 20:44:26 » |
|
Riven, how to you get cookies via this method?
Read document.cookie. Use DOM to insert it into the <applet><param name="myCookie" value="insertHere"/></applet> poll the applet.getParameter("myCookie") in the Applet for change.
|
|
|
|
|