You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Colin Freas <cf...@gmail.com> on 2010/11/30 22:50:34 UTC

execute javascript after action's html renders

On Struts 2.1.6, Dojo and JSON plugins.

I want to execute some JavaScript after an action's HTML finishes loading.

I can't figure out how to make this happen consistently.  The action that
I'm trying to modify is as part of another action.  When I access the child
action directly, the script runs.  When I access it through the parent
action, it doesn't.

There is an attribute "executeScripts" (or similar) set, but...  as near as
I can tell, the script doesn't get run for some reason.

So...  what's the best way to ensure that a script gets run when it's loaded
by an action?  Where might this script be stripped or otherwise disabled
from running?

Any help appreciated.

-Colin

Re: execute javascript after action's html renders

Posted by Colin Freas <cf...@gmail.com>.
Thanks for the replies, Martin and Li.

So, I've tried both of these things (before resorting to the mailing list,
actually), but as I pointed out in my note, the inline <script> tag never
seems to get executed, despite my setting of the *executeScripts* attribute
in the containing action's <sx:div> tag.

Again, I have this containing action, call it *parent.action* with something
like this:

<sx:div href="child.action" executeScripts="true" />


And then in the child, this:

... html/jsp code...
<script>

alert("blah blah blah");

</script>


So, that script never gets run.  Even when I put the dojo.addOnLoad() into
that script...  the script never gets run.

Is there some bug in the executeScripts attribute?  Is there some other
mechanism that might be stripping or otherwise preventing the script from
executing?

Thanks,
Colin



On Wed, Dec 1, 2010 at 9:15 PM, Martin Gainty <mg...@hotmail.com> wrote:

>
> Construct your JSP page with a script for an OnLoad JS function e.g.
>
>    <script>
>        var index = -1;
>        var runningOgnl = true;
>        var ognlBase = "${ognlBase}";
>        var jspBase = "${jspBase}";
>        var ognlCount = 9;
>        var jspCount = 5;
>
>        dojo.addOnLoad(function() {
>            var classSrc = dojo.byId("classSrc");
>            dojo.io.updateNode(classSrc, "${viewClass}");
>            dojo.html.hide("previous");
>            dojo.html.hide("next");
>        });
>
>        dojo.event.topic.subscribe("/reloadGuide", function() {
>            next();
>        });
>
>        function startOgnl() {
>            selectOGNLTab();
>            index = -1;
>            runningOgnl = true;
>            change(1);
>            updateNavigation();
>        }
>
>        function startJSP() {
>            selectJSPTab();
>            index = -1;
>            runningOgnl = false;
>            change(1);
>            updateNavigation();
>        }
>
>        function execOgnl(id) {
>            var exp = dojo.string.trim(dojo.byId(id ? id :
> "example").innerHTML);
>            dojo.byId("wc-command").value = exp;
>
>            keyEvent({keyCode : 13}, '${jspEval}');
>        }
>
>        function execJSP(id) {
>            var exp = dojo.string.trim(dojo.byId(id ? id :
> "example").innerHTML);
>            dojo.byId("jsp").value = unscape(exp);
>
>            dojo.event.topic.publish("/evalJSP")
>        }
>
>        function unscape(str) {
>            return str.replace(/&/gm, "&").replace(/</gm,
> "<").replace(/>/gm, ">").replace(/"/gm, '"');
>        }
>
>        function selectClassSrcTab() {
>            dojo.widget.byId("mainTabContainer").selectTab("classTab");
>        }
>
>        function selectJSPTab() {
>            dojo.widget.byId("mainTabContainer").selectTab("jspTab");
>        }
>
>        function selectOGNLTab() {
>            dojo.widget.byId("mainTabContainer").selectTab("ognlTab");
>        }
>
>        function change(delta) {
>            index+=delta;
>
>            var url = (runningOgnl ? ognlBase : jspBase) + index + ".jsp";
>            var bind = dojo.widget.byId("guideBind");
>            bind.href = url;
>            dojo.event.topic.publish("/loadContent");
>            updateNavigation();
>        }
>
>        function updateNavigation() {
>            if(index <= 0) {
>                dojo.html.hide("previous");
>            } else {
>                dojo.html.show("previous");
>            }
>
>            var top = runningOgnl ? ognlCount : jspCount;
>
>            if(index == top - 1) {
>                dojo.html.hide("next");
>            } else {
>                dojo.html.show("next");
>            }
>        }
>    </script>
>
> Martin Gainty
> ______________________________________________
> Jogi és Bizalmassági kinyilatkoztatás/Verzicht und
> Vertraulichkeitanmerkung/Note de déni et de confidentialité
>
>
> Ez az üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
> jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának készítése
> nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és semmiféle jogi
> alkalmazhatósága sincs.  Mivel az electronikus üzenetek könnyen
> megváltoztathatóak, ezért minket semmi felelöség nem terhelhet ezen üzenet
> tartalma miatt.
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene
> Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte
> Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht
> dient lediglich dem Austausch von Informationen und entfaltet keine
> rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von
> E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le
> destinataire prévu, nous te demandons avec bonté que pour satisfaire
> informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie
> de ceci est interdite. Ce message sert à l'information seulement et n'aura
> pas n'importe quel effet légalement obligatoire. Étant donné que les email
> peuvent facilement être sujets à la manipulation, nous ne pouvons accepter
> aucune responsabilité pour le contenu fourni.
>
>
>
>
>
> > Date: Thu, 2 Dec 2010 09:05:00 +0900
> > Subject: Re: execute javascript after action's html renders
> > From: liying.cn.2010@gmail.com
> > To: user@struts.apache.org
> >
> > Do you mean calling one action, from another action page, using AJAX?
> >
> > Let's say, ActionA is the container page, some AJAX code running in
> > this page, retrieving the response of ActionB, and append the response
> > to the container page.
> >
> > Do I understand it right?
> >
> > In this case, you can embed JavaScript code, in the page code of ActionB,
> likes:
> >
> > ===========================================
> > <html code start>
> > </html code end>
> >
> > <script type="text/javascript">
> > do something here.
> > </script>
> > ===========================================
> >
> > or you can use the "success" event of AJAX.
> > See: http://api.jquery.com/jQuery.ajax/
> > for more information.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>
>

RE: execute javascript after action's html renders

Posted by Martin Gainty <mg...@hotmail.com>.
Construct your JSP page with a script for an OnLoad JS function e.g.
 
    <script>
        var index = -1;
        var runningOgnl = true;
        var ognlBase = "${ognlBase}";
        var jspBase = "${jspBase}";
        var ognlCount = 9;
        var jspCount = 5;
        
        dojo.addOnLoad(function() {
            var classSrc = dojo.byId("classSrc");
            dojo.io.updateNode(classSrc, "${viewClass}");
            dojo.html.hide("previous");
            dojo.html.hide("next");
        });
        
        dojo.event.topic.subscribe("/reloadGuide", function() {
            next();
        });
        
        function startOgnl() {
            selectOGNLTab();
            index = -1;
            runningOgnl = true;
            change(1);
            updateNavigation();
        }
        
        function startJSP() {
            selectJSPTab();
            index = -1;
            runningOgnl = false;
            change(1);
            updateNavigation();
        }
        
        function execOgnl(id) {
            var exp = dojo.string.trim(dojo.byId(id ? id : "example").innerHTML);
            dojo.byId("wc-command").value = exp;
            
            keyEvent({keyCode : 13}, '${jspEval}');
        }
        
        function execJSP(id) {
            var exp = dojo.string.trim(dojo.byId(id ? id : "example").innerHTML);
            dojo.byId("jsp").value = unscape(exp);
            
            dojo.event.topic.publish("/evalJSP")
        }
        
        function unscape(str) {
            return str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<").replace(/&gt;/gm, ">").replace(/&quot;/gm, '"');
        }
        
        function selectClassSrcTab() {
            dojo.widget.byId("mainTabContainer").selectTab("classTab");
        }
        
        function selectJSPTab() {
            dojo.widget.byId("mainTabContainer").selectTab("jspTab");
        }
        
        function selectOGNLTab() {
            dojo.widget.byId("mainTabContainer").selectTab("ognlTab");
        }
       
        function change(delta) {
            index+=delta;
            
            var url = (runningOgnl ? ognlBase : jspBase) + index + ".jsp";
            var bind = dojo.widget.byId("guideBind");
            bind.href = url;
            dojo.event.topic.publish("/loadContent");
            updateNavigation();
        }
        
        function updateNavigation() {
            if(index <= 0) {
                dojo.html.hide("previous");
            } else {
                dojo.html.show("previous");
            }
            
            var top = runningOgnl ? ognlCount : jspCount;
            
            if(index == top - 1) {
                dojo.html.hide("next");
            } else {
                dojo.html.show("next");
            }
        }
    </script>

Martin Gainty 
______________________________________________ 
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

 
Ez az üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.



 

> Date: Thu, 2 Dec 2010 09:05:00 +0900
> Subject: Re: execute javascript after action's html renders
> From: liying.cn.2010@gmail.com
> To: user@struts.apache.org
> 
> Do you mean calling one action, from another action page, using AJAX?
> 
> Let's say, ActionA is the container page, some AJAX code running in
> this page, retrieving the response of ActionB, and append the response
> to the container page.
> 
> Do I understand it right?
> 
> In this case, you can embed JavaScript code, in the page code of ActionB, likes:
> 
> ===========================================
> <html code start>
> </html code end>
> 
> <script type="text/javascript">
> do something here.
> </script>
> ===========================================
> 
> or you can use the "success" event of AJAX.
> See: http://api.jquery.com/jQuery.ajax/
> for more information.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  

Re: execute javascript after action's html renders

Posted by Li Ying <li...@gmail.com>.
Do you mean calling one action, from another action page, using AJAX?

Let's say, ActionA is the container page, some AJAX code running in
this page, retrieving the response of ActionB, and append the response
to the container page.

Do I understand it right?

In this case, you can embed JavaScript code, in the page code of ActionB, likes:

===========================================
<html code start>
</html code end>

<script type="text/javascript">
   do something here.
</script>
===========================================

or you can use the "success" event of AJAX.
See: http://api.jquery.com/jQuery.ajax/
for more information.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: execute javascript after action's html renders

Posted by Colin Freas <cf...@gmail.com>.
So, the "parent" action is using a Dojo *div* tag, with the
*executeScripts* attribute
set to true.

The script in the "child" action, which is referenced in the *div* tag, is
inline JavaScript.

So parent is like:

<sx:div href="child.action" executeScripts="true" />


Child is:

... html/jsp fragments ...
<script>

alert("blah blah blah");

</script>



If I go to child.action from a browser, script runs.  If I got to
parent.action, script doesn't run.

-Colin

On Wed, Dec 1, 2010 at 4:22 PM, Dave Newton <da...@gmail.com> wrote:

> How is the JavaScript implemented in the child action?
>
> And do you mean that an <s:action.../> tag is being used (from a JSP? FM?)
> to load an action (the "child" from above)?
>
> Dave
>
> On Tue, Nov 30, 2010 at 4:50 PM, Colin Freas <cf...@gmail.com> wrote:
>
> > On Struts 2.1.6, Dojo and JSON plugins.
> >
> > I want to execute some JavaScript after an action's HTML finishes
> loading.
> >
> > I can't figure out how to make this happen consistently.  The action that
> > I'm trying to modify is as part of another action.  When I access the
> child
> > action directly, the script runs.  When I access it through the parent
> > action, it doesn't.
> >
> > There is an attribute "executeScripts" (or similar) set, but...  as near
> as
> > I can tell, the script doesn't get run for some reason.
> >
> > So...  what's the best way to ensure that a script gets run when it's
> > loaded
> > by an action?  Where might this script be stripped or otherwise disabled
> > from running?
> >
> > Any help appreciated.
> >
> > -Colin
> >
>

Re: execute javascript after action's html renders

Posted by Dave Newton <da...@gmail.com>.
How is the JavaScript implemented in the child action?

And do you mean that an <s:action.../> tag is being used (from a JSP? FM?)
to load an action (the "child" from above)?

Dave

On Tue, Nov 30, 2010 at 4:50 PM, Colin Freas <cf...@gmail.com> wrote:

> On Struts 2.1.6, Dojo and JSON plugins.
>
> I want to execute some JavaScript after an action's HTML finishes loading.
>
> I can't figure out how to make this happen consistently.  The action that
> I'm trying to modify is as part of another action.  When I access the child
> action directly, the script runs.  When I access it through the parent
> action, it doesn't.
>
> There is an attribute "executeScripts" (or similar) set, but...  as near as
> I can tell, the script doesn't get run for some reason.
>
> So...  what's the best way to ensure that a script gets run when it's
> loaded
> by an action?  Where might this script be stripped or otherwise disabled
> from running?
>
> Any help appreciated.
>
> -Colin
>