You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Tedy Marcos Colombini <te...@gmail.com> on 2009/03/30 16:27:20 UTC

executing inline scripts from ajax responses (dojo 0.4)

Hi there,

After being able to submit an ajax form request using javascript, I am stuck
in the second (and last) problem. The response contains an inline script
which I wanna execute when the it is rendered. I am using Struts 2.0.11 and
Dojo 0.4. I did some research but everything I found is incomplete.

This is the code. It works, only the alert from the response doesn't pop
out. So what I should do to make this script run?


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <s:head theme="ajax" debug="true"/>
    <script type="text/javascript">
        function submitForm(formId, divId) {
        var kw = {
            formNode: dojo.byId(formId),
            mimetype: "text/plain",
            method: "post",
            transport: "XMLHTTPTransport",
            load: function(type, data, http, kwArgs){
                dojo.byId(divId).innerHTML = data;
            },
            error: function(type, error, http){
                alert(error);
             }
        };
        dojo.io.bind(kw);
    }
    </script>
</head>

<body>
    <s:form id="myForm" action="example/Test.action">
        <input type="button" onclick="submitForm('myForm', 'divResult')"
value="Javascript" />
    </s:form>
    <div id="divResult"></div>
</body>
</html>


This is the response from Test.action:

Content
<script>
    alert("test")
</script>


Thank you for your help,

-- 
Tedy Marcos Colombini

Re: executing inline scripts from ajax responses (dojo 0.4)

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
I take it what you want to do is insert "Content" into divId, and then
fire an alert().  The problem you have is that the value of data is
nothing but a simple string.  Your callback has to know what to do with
it.

IIRC, Dojo has a function that will execute all <script> tags within a
string, so you could certainly call that to get the alert() to fire... but
that doesn't help you put the content in the DOM.

I suggest what you should do instead is have your response returned a bit
differently.  Something as simple as this:

Content~~test

Then in your callback you do:

var a = data.split("~~");
document.getElementById(divId).innerHTML = a[0];
alert(a[1]);

The response could also be JSON:

{ divContent: "Content", msg : "test" }

Then you do:

data = eval(data);
document.getElementById(divId).innerHTML = data.divContent;
alert(data.msg);

hth,
Frank

-- 
Frank W. Zammetti
Author of "Practical Ext JS Projects with Gears" (coming soon)
  and "Practical Dojo Projects"
  and "Practical DWR 2 Projects"
  and "Practical JavaScript, DOM Scripting and Ajax Projects"
  and "Practical Ajax Projects with Java Technology"
  (For info: apress.com/book/search?searchterm=zammetti&act=search)
All you could possibly want is here: zammetti.com

On Mon, March 30, 2009 10:27 am, Tedy Marcos Colombini wrote:
> Hi there,
>
> After being able to submit an ajax form request using javascript, I am
> stuck
> in the second (and last) problem. The response contains an inline script
> which I wanna execute when the it is rendered. I am using Struts 2.0.11
> and
> Dojo 0.4. I did some research but everything I found is incomplete.
>
> This is the code. It works, only the alert from the response doesn't pop
> out. So what I should do to make this script run?
>
>
> <%@ page contentType="text/html; charset=UTF-8" %>
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <html>
> <head>
>     <s:head theme="ajax" debug="true"/>
>     <script type="text/javascript">
>         function submitForm(formId, divId) {
>         var kw = {
>             formNode: dojo.byId(formId),
>             mimetype: "text/plain",
>             method: "post",
>             transport: "XMLHTTPTransport",
>             load: function(type, data, http, kwArgs){
>                 dojo.byId(divId).innerHTML = data;
>             },
>             error: function(type, error, http){
>                 alert(error);
>              }
>         };
>         dojo.io.bind(kw);
>     }
>     </script>
> </head>
>
> <body>
>     <s:form id="myForm" action="example/Test.action">
>         <input type="button" onclick="submitForm('myForm', 'divResult')"
> value="Javascript" />
>     </s:form>
>     <div id="divResult"></div>
> </body>
> </html>
>
>
> This is the response from Test.action:
>
> Content
> <script>
>     alert("test")
> </script>
>
>
> Thank you for your help,
>
> --
> Tedy Marcos Colombini
>



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


Re: executing inline scripts from ajax responses (dojo 0.4)

Posted by Tedy Marcos Colombini <te...@gmail.com>.
Tks guys for your help!

Andras, I saw this stuff before and it doesn't work. At least I tried to
use a content pane with *executeScripts* = true and *scriptSeparation* =
false and nothing happened.

In fact, my problem is not with an alert. I just create a simple use case to
illustrate the problem I have in my project. Anyways, your responses were
very useful! I will try some of them and as soon as I make it work, I post
the solution here.

Cheers,

Tedy


On Mon, Mar 30, 2009 at 5:17 PM, Andras Balogh <an...@reea.net> wrote:

> Hi,
>
> You could try to use the ContentPane Widget and load the response into it
> however I did not try this
> so I'm not 100% sure if it will work.
> See here:
>
> http://dojotoolkit.org/book/dojo-book-0-4/part-4-more-widgets/advanced-contentpane-usage
> and notice the "executeScripts" property.
>
> BR,
> Andras.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Tedy Marcos Colombini

Re: executing inline scripts from ajax responses (dojo 0.4)

Posted by Andras Balogh <an...@reea.net>.
Hi,

You could try to use the ContentPane Widget and load the response into 
it however I did not try this
so I'm not 100% sure if it will work.
See here:
http://dojotoolkit.org/book/dojo-book-0-4/part-4-more-widgets/advanced-contentpane-usage
and notice the "executeScripts" property.

BR,
Andras.

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


Re: executing inline scripts from ajax responses (dojo 0.4)

Posted by Tedy Marcos Colombini <te...@gmail.com>.
Hi Andras,

Sorry to post here, I understand this is related to Dojo but its
documentation/forum sucks. They haven't replied to this question so far and
I decided to get help here. Also, I saw a lot of doubts about this same
problem but not even one good response and I guess this is not something
extremely difficult.

Anyways, in my response I have a mix between script code that should be
executed and content that should be displayed. So the only option is the
eval function?
Cheers,

Tedy


On Mon, Mar 30, 2009 at 4:56 PM, Andras Balogh <an...@reea.net> wrote:

> Hi,
>
>   This is a little bit off-topic because it's related more to Dojo than
> Struts.
> But anyway: do you get back something in your "data" object in function
> load()?
> If you know that you are returning javascript do not send the <script> tag
> (only the javascript)
> and use eval(data) instead of innerHTML.
>
> BR,
> Andras
>
>
> Tedy Marcos Colombini wrote:
>
>> Hi there,
>>
>> After being able to submit an ajax form request using javascript, I am
>> stuck
>> in the second (and last) problem. The response contains an inline script
>> which I wanna execute when the it is rendered. I am using Struts 2.0.11
>> and
>> Dojo 0.4. I did some research but everything I found is incomplete.
>>
>> This is the code. It works, only the alert from the response doesn't pop
>> out. So what I should do to make this script run?
>>
>>
>> <%@ page contentType="text/html; charset=UTF-8" %>
>> <%@ taglib prefix="s" uri="/struts-tags" %>
>> <html>
>> <head>
>>    <s:head theme="ajax" debug="true"/>
>>    <script type="text/javascript">
>>        function submitForm(formId, divId) {
>>        var kw = {
>>            formNode: dojo.byId(formId),
>>            mimetype: "text/plain",
>>            method: "post",
>>            transport: "XMLHTTPTransport",
>>            load: function(type, data, http, kwArgs){
>>                dojo.byId(divId).innerHTML = data;
>>            },
>>            error: function(type, error, http){
>>                alert(error);
>>             }
>>        };
>>        dojo.io.bind(kw);
>>    }
>>    </script>
>> </head>
>>
>> <body>
>>    <s:form id="myForm" action="example/Test.action">
>>        <input type="button" onclick="submitForm('myForm', 'divResult')"
>> value="Javascript" />
>>    </s:form>
>>    <div id="divResult"></div>
>> </body>
>> </html>
>>
>>
>> This is the response from Test.action:
>>
>> Content
>> <script>
>>    alert("test")
>> </script>
>>
>>
>> Thank you for your help,
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Tedy Marcos Colombini

Re: executing inline scripts from ajax responses (dojo 0.4)

Posted by Andras Balogh <an...@reea.net>.
Hi,

    This is a little bit off-topic because it's related more to Dojo 
than Struts.
But anyway: do you get back something in your "data" object in function 
load()?
If you know that you are returning javascript do not send the <script> 
tag (only the javascript)
and use eval(data) instead of innerHTML.

BR,
Andras

Tedy Marcos Colombini wrote:
> Hi there,
>
> After being able to submit an ajax form request using javascript, I am stuck
> in the second (and last) problem. The response contains an inline script
> which I wanna execute when the it is rendered. I am using Struts 2.0.11 and
> Dojo 0.4. I did some research but everything I found is incomplete.
>
> This is the code. It works, only the alert from the response doesn't pop
> out. So what I should do to make this script run?
>
>
> <%@ page contentType="text/html; charset=UTF-8" %>
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <html>
> <head>
>     <s:head theme="ajax" debug="true"/>
>     <script type="text/javascript">
>         function submitForm(formId, divId) {
>         var kw = {
>             formNode: dojo.byId(formId),
>             mimetype: "text/plain",
>             method: "post",
>             transport: "XMLHTTPTransport",
>             load: function(type, data, http, kwArgs){
>                 dojo.byId(divId).innerHTML = data;
>             },
>             error: function(type, error, http){
>                 alert(error);
>              }
>         };
>         dojo.io.bind(kw);
>     }
>     </script>
> </head>
>
> <body>
>     <s:form id="myForm" action="example/Test.action">
>         <input type="button" onclick="submitForm('myForm', 'divResult')"
> value="Javascript" />
>     </s:form>
>     <div id="divResult"></div>
> </body>
> </html>
>
>
> This is the response from Test.action:
>
> Content
> <script>
>     alert("test")
> </script>
>
>
> Thank you for your help,
>
>   


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