0

I have a grails resource module set up:

modules {
    myModule {
        resources url:'js/application.js'
        resources url:'js/helper.js'
    }
}

And I can load this onto my GSP page using <r:require ... />.

Problem:

I want some JS code to fire when this script has loaded. Is there anyway to do this?

Even if I could do it via JS (script.onLoad) that would be ok, I'm just not sure how to generate a link to the module using grails.

1 Answer 1

3

You can include your module in your GSP page as:

<r:require modules="myModule" />

and you can fire any java script function in you js files on page load.

in case your module will not get loaded on the GSP, try adding disposition, i.e.

modules {
    myModule {
        resources url:'js/application.js', disposition: 'head'
        resources url:'js/helper.js', disposition: 'head'
    }
}

I think better way to include your resources on the view pages, is through layout, I used to do it like, adding all the resources on the layout page, ex. layout->main.gsp

add all the required resources on this page as:

<r:require
    modules="myModule" />

<g:layoutHead />
<r:layoutResources />

then on all the GSP pages in the application you can apply this layout, i.e.

<g:applyLayout name="main"/>

and to call any java script code(on the included js files) on script load, I think you should associate it with the onload event on the gsp page.

Sign up to request clarification or add additional context in comments.

1 Comment

Sweet, thanks. I went with disposition 'defer' in the end but got it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.