0

I want to include a javascript file custom.js in my gsp from a plugin. My main app uses this plugin as a dependency. The custom.js javascript file contains a variable whose value is obtained by executing grails code within GString.

var root = "${request.contextPath}";

It has to be a separate file and if I try to include it inside my .gsp file using <script src="custom.js"></script> the grails code doesn't get executed. If I use <g:javascript src="custom.js" /> it tries to fetch the file from within the plugin and cannot find it. The file is actually in my main application. But the code that's trying to include it is in the plugin. Something like a way of providing custom Javascript implementation. How do I go about this?

Merhawi Fissehaye
  • 2,482
  • 2
  • 25
  • 39

1 Answers1

0

Update Taking this answer into consideration I have to specify the value of the variable in a separate scrip tag. No grails code is interpreted if it's in a non-.gsp file.

I solved it. All I had to do was: add contextPath="" into the g:javascript tag

According to the documentation of the tag g:javascript about the contextPath attribute:

contextPath (optional) - the context path to use (relative to the application context path). Defaults to "" or path to the plugin for a plugin view or template.

It defaults to the path to the plugin. So by setting it to empty string we tell it to avoid the plugin path from the source. Previously it modified the paths to the javascript to something like appname/plugins/pluginname/js/custom.js. Now with contextPath set to empty string it simply makes the path appname/js/cusomt.js

Community
  • 1
  • 1
Merhawi Fissehaye
  • 2,482
  • 2
  • 25
  • 39