change session-variable inside javascript function

799 views
Skip to first unread message

john.p...@gmail.com

unread,
Jul 16, 2013, 4:16:54 AM7/16/13
to gra...@googlegroups.com
Hey folks,
I'm having a sessionvariable

<g:set var="lang" value="de" scope="session"/>

and want to change the value of this variable inside a javascript function:

function changeLang(language) {
        "${session.lang }" = language
        location.reload()
    }

called from a simple

<a href="" onclick="changeLang('en');" />EN</a>
<a href="" onclick="changeLang('de');" />DE</a>

This one doesn't work because of the qoutation sign around ${session.lang}
Is there any way to solve that issue?
Thanks so far
John

dspies

unread,
Jul 17, 2013, 4:51:15 PM7/17/13
to gra...@googlegroups.com
Hi John,
It looks like you are trying to set a GSP variable with javascript, but that is not possible since the GSP is rendered server-side before it is served to the client as HTML and javascript.  You can, however, just work with the 'lang' variable in javascript, storing it in localStorage, cookie, etc to persist it for the session.  

On a side note, if you are doing internationalization work, I believe it is generally recommended to do a page refresh to re-render your page with the appropriate language.  Here is a page from stackflow that gives a good description of why: http://stackoverflow.com/questions/11676558/i18n-server-side-vs-client-side.  If you choose to use a page refresh, you can change your <a href...> to a small form that posts back to the server and you can store the language in the session on the server.
Reply all
Reply to author
Forward
0 new messages