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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -