2

i can use grails variable into javascript like this in my gsp file:

<g:javascript>
var id= ${personInstance?.id} ; 
alert(id) ;
</g:javascript>

but this doesn't work when i use an imported js file like this :

<g:javascript src="test.js"></g:javascript>

test.js :

 var id= ${personInstance?.id} ; 
 alert(id) ;

How can i use grails variable inside imported javascript file ??

2 Answers 2

4

GSPs are server side components: if you put some js inside them, groovy variables will be resolved. An external js is retrieved by the browser, on client side. For this reason, AFAIK, you can not do the thing you're trying to do.

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

1 Comment

I just keep all of the variables that need server side values separate.
2

You could generate test.js in your grails application or (probably much better) generate an object or JSON with all of the variables you need and pass it into the functions in the external JavaScript file.

Comments

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.