Grails application context in javascript library

Is it possible to access

grails.app.context 

from javascript library? - This is not javascript inserted in the GSP file.

I have som javascripts that are context sensitive, so I need to have access to this from javascript.

I am including javascript in my gsp files with:

 <r:require modules="myModule" /> 
+4
source share
1 answer

You can pass it Javascript as a global JS variable, put this inside the <head> :

 <g:javascript> window.appContext = '${request.contextPath}'; </g:javascript> 

and use it anywhere from simple javascript, for example:

 $.ajax({ url: appContext + '/hello/world' }) 
+8
source

Source: https://habr.com/ru/post/1415101/


All Articles