Where should I put javascript libraries in a Grails app?

17,730

Solution 1

With your JS file at: web-app/js/myLib.js, putting <g:javascript library="myLib" /> in your layout should I think be what you need.

Solution 2

There are two ways by which you can include JS file and JS library like scriptaculous

// actually imports '/app/js/myscript.js'
<g:javascript src="myscript.js" />

// imports all the necessary js for the scriptaculous library
<g:javascript library="scriptaculous" />

<g:javascript>alert('hello')</g:javascript>

Refer : http://grails.org/doc/latest/ref/Tags/javascript.html

Share:
17,730

Related videos on Youtube

Simon
Author by

Simon

Updated on April 15, 2022

Comments

  • Simon
    Simon about 2 years

    I have a couple of Javascript libraries of my own that do some nice prettyfying of my HTML pages and I want to include them in my gsp pages, particularly the landing page of my app. I have tried putting them in views folder and web-app/js and $APP_HOME/scripts but when I load my index.gsp the scripts don't show up.

    I have also tried a variety of alternatives in my code none of which work...

    <script src="mylib.js" type="text/javascript"></script>
    <script src="js/mylib.js" type="text/javascript"></script>
    <script src="scripts/mylib.js" type="text/javascript"></script>
    

    I'm sure there is a clever grails way of doing this on the fly, but I really just want a location where I can place some boilerplate JavaScript code that I can use in my app. With convention over configuration in mind, what is the expected practice?

  • Will Prescott
    Will Prescott over 14 years
    yep exactly - I need to type more quickly :)
  • Rick Mangi
    Rick Mangi over 11 years
    see below: this is wrong. <g:javascript src="myLib" /> is correct.
  • chelder
    chelder over 10 years
    You forgot js here @RickMangi. The correct way is: <g:javascript src="myLib.js" />
  • Abhilash Shajan
    Abhilash Shajan almost 7 years
    What about in grails 3 ?
  • RPDeshaies
    RPDeshaies over 6 years
    What happens when you build and deploy your app ? Is the JavaScript automatically packaged ???