JavaScript How to send Javascript code from Controller to GSP view? send email javascript code,send mail javascript code,send email

I want to send some JS Script from controller to GSP view . I do the following attempt.

Controller(Purchase.groovy) :

 def myaction={
     flash.script= 'jQuery("div#header").show(1000);'
     redirect(action:'edit')
}

In purchase/myaction.gsp file, I try the 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 also : jQuery.getScript('${flash.script}') instead of '${flash.script}' However, GSP page renders always the second script(else statement)

Answer:1

First of all, be sure to call the function after the dom loaded; u can use jquery like

  $( document ).ready(function() {
     call_function();
  });

And you can send js code form controller as map, not through flash and redirecting to another 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 %>
})

Answer:2

I am making a chrome extension that will take come content from the current selected tab. I am injecting a script file into the current page and that script communicates with the script from the ...

I am making a chrome extension that will take come content from the current selected tab. I am injecting a script file into the current page and that script communicates with the script from the ...

I have the following code in my javascript. I'd like the alert to happen wnen a user checks the radio button, but instead it happens when the page loads, and NOTHING happens when the radio button ...

I have the following code in my javascript. I'd like the alert to happen wnen a user checks the radio button, but instead it happens when the page loads, and NOTHING happens when the radio button ...

So this is what I am after, and I've been told it isn't possibly but I am not going to give up just yet! Let's say the user types in "London" into my location search box and clicks "Geocode" I am ...

So this is what I am after, and I've been told it isn't possibly but I am not going to give up just yet! Let's say the user types in "London" into my location search box and clicks "Geocode" I am ...

First and foremost, I apologize in advance for the ignorance on my part - I do not know JavaScript at all and I am somewhat new to Stackoverflow and jsFiddle. I did my homework and was able to get the ...

First and foremost, I apologize in advance for the ignorance on my part - I do not know JavaScript at all and I am somewhat new to Stackoverflow and jsFiddle. I did my homework and was able to get the ...

  1. applying javascript to html