0

When I tried to set a variable from javascript function in GSP view I had error: "Cannot invoke method func() on null object"

GSP:

<g:set var="variable" value="${tr.func.name()}" />

I also tried to the get around the problem without success: variable is undifined

<g:javascript>var test = tr.func.name();</g:javascript>
<g:set var="variable" value="${test}" />
<g:javascript>alert(${variable});</g:javascript>

Thanks

3
  • 1
    it's impossible. javascript works in browser, on client computer, not server, when view is already processed and transferred to browser. what you really trying to reach? Commented Feb 6, 2014 at 13:41
  • I have a jquery function that get timzone from the user, I want to use it for: <g:formatDate format="yyyy-MM-dd HH:mm" date="${date}" timeZone="GMT"/> Commented Feb 6, 2014 at 13:49
  • 1
    it's too late at this moment, gsp works on server. you cannot use gsp tags in browser. so, you have to use javascript for date formatting. Commented Feb 6, 2014 at 13:52

2 Answers 2

2

You should use Javascript in browser. GSP works on server side.

Just pass date in GMT to browser, and apply formatting there. There is a good js library for Date formatting: moment.js

Sign up to request clarification or add additional context in comments.

Comments

1

You can't do that. <g:set> is used to set server-side variables within the GSP. Javascript is client-side.

2 Comments

Is there another solution provided by Grails?
There's no solution full-stop. The GSP tags are rendered prior to being sent to the client. When the JavaScript is executed it is in the client. The distinction between client and server side is an important one. The solution you are looking for is to use JavaScript date formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.