grails - How to send Javascript code from Controller to GSP view? -
i want send js script controller gsp view . following attempt.
controller(purchase.groovy) :
def myaction={ flash.script= 'jquery("div#header").show(1000);' redirect(action:'edit') }
in purchase/myaction.gsp file, try following code
<g:if test="${flash.script !=null}"> <g:javascript> $(function() { ${flash.script} }) </g:javascript> </g:if> <g:else> <g:javascript> $(function() { alert('welcome') }) </g:javascript> </g:else>
i try : jquery.getscript('${flash.script}')
instead of '${flash.script}'
however, gsp page renders second script(else statement)
first of all, sure call function after dom loaded; u can use jquery like
$( document ).ready(function() { call_function(); });
and can send js code form controller map, not through flash , redirecting action. see below:
def myaction={ redirect(action:'edit', customjs: 'jquery("div#header").show(1000);') } def edit={ render view: 'someview', model:[customjs: customjs] }
and in view:
<g:javascript> $(function() { <%= customjs %> })
Comments
Post a Comment