(Quick Reference)
javascript
Purpose
Allows inclusion of javascript libraries and scripts as well a shorthand for inline Javascript. Specifying a library tells the Grails AJAX tags which JS provider to use.
This tag is Resources plugin aware, and will defer to resources plugin to generate links and include javascript as and when necessary.
Examples
// 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>Description
If you do not include a "library" or "src" attribute but instead provide a body, the result is an inline script.
If the Resources plugin is installed and a body is used to produce inline script, this will defer to the <r:script> tag to produce inline script that is included at the end of the body. For more control use the
resources plugin's <r:script> tag directly.
Attributes
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.
-
library (optional) - The name of the library to include. Either "prototype", "scriptaculous", "yahoo" or "dojo". If Resources plugin is installed, no link to the library will be rendered immediately. Rather, it will include the resource module with the same name as the library and let Resources do the rest. This means you must have a resource module declared with the same name, as if you had used an <r:use module="jquery"/> tag.
-
src (optional) - The name of the javascript file to import. Will look in /app/js dir
-
base (optional - Since 0.6) - specifies the full base url to prepend to the library name
plugin (optional) - The plugin to look for the javascript in
Source
Show Source
def javascript = { attrs, body -> setUpRequestAttributes() if (attrs.src) {
javascriptInclude(attrs)
}
else if (attrs.library) {
if (resourceService) {
out << r.require(module:attrs.library)
} else {
if (LIBRARY_MAPPINGS.containsKey(attrs.library)) {
LIBRARY_MAPPINGS[attrs.library].each {
if (!request[INCLUDED_JS].contains(it)) {
request[INCLUDED_JS] << it
def newattrs = [:] + attrs
newattrs.src = it + '.js'
javascriptInclude(newattrs)
}
}
if (!request[INCLUDED_LIBRARIES].contains(attrs.library)) {
request[INCLUDED_LIBRARIES] << attrs.library
}
}
else {
if (!request[INCLUDED_LIBRARIES].contains(attrs.library)) {
def newattrs = [:] + attrs
newattrs.src = newattrs.remove('library') + '.js'
javascriptInclude(newattrs)
request[INCLUDED_LIBRARIES] << attrs.library
request[INCLUDED_JS] << attrs.library
}
}
}
}
else {
if (resourceService) {
r.script([disposition:'head'], body)
} else {
out.println '<script type="text/javascript">'
out.println body()
out.println '</script>'
}
}
} private javascriptInclude(attrs) {
def requestPluginContext
if (attrs.plugin) {
requestPluginContext = pluginManager.getPluginPath(attrs.remove('plugin')) ?: ''
}
else {
if (attrs.contextPath != null) {
requestPluginContext = attrs.remove('contextPath').toString()
}
else {
requestPluginContext = pageScope.pluginContextPath ?: ''
}
} if (attrs.base) {
attrs.uri = attrs.remove('base') + attrs.remove('src')
} else {
def appBase = grailsAttributes.getApplicationUri(request)
if (!appBase.endsWith('/')) {
appBase += '/'
}
def reqResCtx = ''
if (requestPluginContext) {
reqResCtx = (requestPluginContext.startsWith("/") ? requestPluginContext.substring(1) : requestPluginContext) + '/'
}
attrs.uri = appBase + reqResCtx + 'js/'+attrs.remove('src')
}
out << g.external(attrs)
}