Tuesday, June 8, 2010

including javascript in gsp

Today I have tried out at least a hundred different ways for including js files into my main.gsp by hand. My approach looked like that:



I tried ${resource} first, but then I found this in grails Q&A:

Q: Why won't XXX Javascript Library Display Properly?

A: Sometimes third party javascript libraries depend upon other resources loaded directly from the javascript files themselves like stylesheets. Examples of libraries that do this would be the Yahoo UI libraries and niftycube. When the javascript attempts to load the URL Grails changes the URL so that the resource is not found. Try adding a link using the createLinkTo tag for the particular resource.

So it tried ${createLink}, but in the end nothing worked. I finally succeeded when I discovered that there was an extra taglib to do the trick with YUI JS files, which I was interested in:



The solution can be found in the most obvious place: http://www.grails.org/YUI+Plugin

Really wish I had looked there sooner :-)

Now I wonder if the problem was YUI specific and if my approach would work on other files. Maybe I'm going to find out some day...

No comments:

Post a Comment