View Full Version : Setting a cookie in script
antun
06-10-2003, 03:51 PM
You can set a cookie using LZX by performing the assignment in client-side (i.e. browser) JavaScript. By prefixing the URL of a loadURL() with javascript:
<canvas>
<simplelayout spacing="5"/>
<text>Enter cookie value below:</text>
<windowtext name="cookieValue">bar</windowtext>
<button>Set Cookie
<method event="onclick">
// Build up a string like:
// java script: document.cookie = "foo=bar"; void(0);
var s ='javascript:';
s += 'document.cookie="foo='+ cookieValue.getText();
s += '"; void(0);';
LzBrowser.loadURL( s );
</method>
</button>
<button>Get Cookie
<method event="onclick">
LzBrowser.loadURL('javascript:alert(document.cooki e)');
</method>
</button>
</canvas>
Enjoy!
Please note: For security reasons, the forum software automatically splits javascript into two words. In the example code above, javascript should be written without the space.
bernardsirius
11-17-2004, 10:32 AM
did you notice this trick didn't work with ie?
don't you forget that ie is still 95% of the browser "market"
bernard
laszfellow
10-06-2006, 05:57 AM
Originally posted by antun
<button>Get Cookie
<method event="onclick">
LzBrowser.loadURL('javascript:alert(document.cooki e)');
</method>
</button>
I don't think I quite understand the features of LzBrowser.
In the example, Antun recommended using
LzBrowser.loadURL(
'javascript:alert(document.cookie)');
Could we might as well used
LzBrowser.loadJS('alert(document.cookie)');
? What's the difference?
laszfellow
10-06-2006, 06:06 AM
Originally posted by bernardsirius
did you notice this trick didn't work with ie?
don't you forget that ie is still 95% of the browser "market"
bernard
Really? That's dissappointing.
That was in 2004, now is 2006 - need to try this out and verify if it won't work on IE still.
hipik
01-22-2008, 08:41 AM
Is there anyone that can help out with this issue???
hipik
01-22-2008, 08:43 AM
Is there anyone that can help out with this issue???
Plus I need to do all this in a popup Browser so I guess with
LzBrowser.loadJS(...);
antun
01-22-2008, 08:52 AM
You're correct; loadJS() can be used instead of loadURL("javascript:..."). I think when I wrote my original post, loadJS() didn't yet exist.
-Antun
Could we might as well used
LzBrowser.loadJS('alert(document.cookie)');
? What's the difference?
antun
01-22-2008, 08:57 AM
I'm not clear what the problem is? loadJS() should work reliably for executing browser JavaScript.
-Antun
Is there anyone that can help out with this issue???
hipik
01-22-2008, 10:31 AM
Thanks for the response Antun however my problem persists;
I need a sample code that popups browser window and
that sends two header params along with the provided URL in
the JS "window.open" method, I achieve this feature easily (all but popup)
through laszlo when I use dataset for my http request here is the code snippet:
this.myds.setQueryParams(null);
var p = new LzParam();
p.addValue("query" , someserializedxml, true );
this.myds.setQueryString( p );
this.myds.setHeader("cookie","JSESSIONID="+canvas['sessioniddataset'].getPointer().xpathQuery('sessionid/text()'));
this.myds.setHeader("connection","keep-alive");
this.myds.doRequest();
Now I need pretty much the same url called in popup browser but with the same
headers set!!!
Technical use of this is to have serialized PDF as response from backend server
in a separate browser window in a from of some kind of report that would be opened
by acrobat reader plugin and therefore easily printed!!!
And since this is TIPS'n'TRICKS I'm very proud to say that this code snippet is a very cool way to maintain sessions throughout laszlo app
My Laslo Server is 4.0.2
Please help!
antun
02-20-2008, 04:04 PM
Sorry I didn't get back to you sooner. If I understand you correctly, here's what I'd recommend:
- Write a browser JS function in the HTML source for your popup window to set a cookie on itself and load the right document. It can take all the paramaters it needs as function arguments. (Call it, say loadPDF()).
- Write a browser JS function in the HTML wrapper page for your OpenLaszlo application. Call it, say loadPDFInChildWindow(). This function will call the loadPDF() function in the child window.
- Have your OpenLaszlo application call loadPDFInChildWindow(), using LzBrowser.loadJS().
-Antun
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.