You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jukka Välimaa <va...@gmail.com> on 2008/04/16 10:21:54 UTC
Trouble executing scripts in returned ajax content
Hi everyone,
I'm using Struts 2.1's ajax tags to replace content in a section of my page.
I have this section enclosed in sx:div tags, like so:
<sx:div id="pageContentSection" preload="false" href="%{myUrl}"
listenTopics="/reloadContentSection"
afterNotifyTopics="/contentSectionLoaded" executeScripts="true"
showLoadingText="true" separateScripts="true" indicator="barIndicator">
..
</sx:div>
I change the div content by editing its href with javascript and then
publishing the topic it's listening to:
function reloadContentSection(url){
if(!isAnUrl(url)) return false;
var contentSection = dojo.widget.byId("pageContentSection");
if(contentSection == null) return false;
var origHref = contentSection.href;
contentSection.href = url;
dojo.event.topic.publish('/reloadContentSection', '','');
contentSection.href =origHref;
}
It's working fine otherwise, but I have trouble executing scripts in
returned content. I've experimented with different values of executeScripts
and separateScripts to no effect. I've even tried to include scripts as a
reference to separate javascript library--no better luck there. Even though
scripts I write don't work, a datetimepicker in the same returned content,
done using ajax tags and of course relying on scripts, works perfectly.
When I set separateScripts as false, my scripts are included in generated
DOM source, after some scripts by Matt Cruse, which I assume are directly
related to struts ajax functionality. Of course, being included doesn't mean
that they work.
Right now, I've worked around this problem by including the scripts I need
in the page I'm calling ajax from, and not in returned content. I don't
think this is good design, and won't work if I need to generate scripts
dynamically.
Can any of you tell me what's wrong, or how to fix it?
Jukka
Re: Trouble executing scripts in returned ajax content
Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Jukka Välimaa wrote:
> Thank you for your time Jeromy,
>
> I have confirmed that the problem with running javascript is actually
> related to ajax tags. I placed the following script on my page:
> <script type="text/javascript" >
> alert('boo');
> </script>
>
> It refused to run until I removed the one <sx:div> section I had in the
> page:
> <sx:div id="pricingParams" href="%{#ajaxPricingUrl}"
> listenTopics="/reloadPricingParams"
> indicator="pricingParamsIndicator"
> preload="true" executeScripts="true" separateScripts="true">
> <s:include value="ajaxPricing.jsp" />
> </sx:div>
>
> After I removed that, the script worked fine. As far as I can tell, any ajax
> tags, even a simple sx:a, stops scripts from running. At the moment, I'm
> using a 2.1.1 snapshot version of the dojo plugin; do you recommend that I
> change to another version? To another poster, you recommended not using ajax
> tags.
>
hmm... I did something similar and confirmed that it was working.
However, I used the bind tag rather than listenTopics, the target div
was empty and I did not use the preload attribute. I recommend creating
a small test application and trying it in isolation. I agree there is
something wrong. When I get time I'll take another look. I used 2.1.1.
My code:
<s:submit value="Submit" id="submit3" />
<sx:bind targets="div1" highlightColor="#ffffcc" highlightDuration="500" sources="submit3"
events="onclick" href="%{#fragment3Url}" errorNotifyTopics="/error" executeScripts="true"
separateScripts="true"/>
Make sure you switch dojo over to use the uncompressed version via the
head tag.
You can see the javascript code of the Bind widget is self-evident and
that's why it works:
// no validation or validation passed
if(this.executeScripts) {
//parse text to extract content and javascript
var parsed = this.parse(data);
//update targets content
this.setContent(parsed.text);
//execute scripts
this._executeScripts(parsed.scripts);
}
else {
this.setContent(data);
}
The operation of the div (bindDiv) is not so obvious however. It's
nasty. I'd have to trace through it in your example to see where it gets to.
In general, this is why I don't like tags that hide the underlying
client-side library. By using the javascript library directly you get a
few better appreciation for how it works, it's much easier to debug and
you can receive support from the library's community of developers.
It's an illusion that the tags increase productivity because any
non-trivial application will need to access the API of the underlying
library eventually.
regards,
Jeromy Evans
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Trouble executing scripts in returned ajax content
Posted by Jukka Välimaa <va...@gmail.com>.
Thank you for your time Jeromy,
I have confirmed that the problem with running javascript is actually
related to ajax tags. I placed the following script on my page:
<script type="text/javascript" >
alert('boo');
</script>
It refused to run until I removed the one <sx:div> section I had in the
page:
<sx:div id="pricingParams" href="%{#ajaxPricingUrl}"
listenTopics="/reloadPricingParams"
indicator="pricingParamsIndicator"
preload="true" executeScripts="true" separateScripts="true">
<s:include value="ajaxPricing.jsp" />
</sx:div>
After I removed that, the script worked fine. As far as I can tell, any ajax
tags, even a simple sx:a, stops scripts from running. At the moment, I'm
using a 2.1.1 snapshot version of the dojo plugin; do you recommend that I
change to another version? To another poster, you recommended not using ajax
tags.
I'm asking since at the version of the plugin I'm using, the tags seem to be
highly breakable; they give problems that are very difficult to debug.
On Sat, Apr 19, 2008 at 9:46 AM, Jeromy Evans <
jeromy.evans@blueskyminds.com.au> wrote:
> Jukka Välimaa wrote:
>
> > Thanks for input,
> >
> > Yes, I have debug on. Interestingly enough, with firefox it gives me
> > just
> > this:
> > DEBUG: Error running scripts from content
> >
> > However, I just noticed that with IE, I get this:
> > DEBUG: Error running scripts from content:Could not complete the
> > operation
> > due to error 80020101
> >
> >
> >
>
> Hi Jukka,
>
> I have confirmed that the plugin will execute inline scripts if (and only
> if) executeScripts="true" and separateScripts="true".
>
> The errors you're getting are caused by invalid javascript evaluation.
> 80020101 is probably a missing semi-colon.
>
> Use a javascript delinter to find the javascript errors:
> http://www.jslint.com/lint.html
> http://www.javascriptlint.com/
>
> Your work-around is fine. In fact, I discourage any use of inline scripts
> and recommend binding to the events published by the dojo widgets.
> regards,
> Jeromy Evans
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: Trouble executing scripts in returned ajax content
Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Jukka Välimaa wrote:
> Thanks for input,
>
> Yes, I have debug on. Interestingly enough, with firefox it gives me just
> this:
> DEBUG: Error running scripts from content
>
> However, I just noticed that with IE, I get this:
> DEBUG: Error running scripts from content:Could not complete the operation
> due to error 80020101
>
>
Hi Jukka,
I have confirmed that the plugin will execute inline scripts if (and
only if) executeScripts="true" and separateScripts="true".
The errors you're getting are caused by invalid javascript evaluation.
80020101 is probably a missing semi-colon.
Use a javascript delinter to find the javascript errors:
http://www.jslint.com/lint.html
http://www.javascriptlint.com/
Your work-around is fine. In fact, I discourage any use of inline
scripts and recommend binding to the events published by the dojo widgets.
regards,
Jeromy Evans
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Trouble executing scripts in returned ajax content
Posted by Jukka Välimaa <va...@gmail.com>.
Thanks for input,
Yes, I have debug on. Interestingly enough, with firefox it gives me just
this:
DEBUG: Error running scripts from content
However, I just noticed that with IE, I get this:
DEBUG: Error running scripts from content:Could not complete the operation
due to error 80020101
At the moment, I'm working around this problem by writing the scripts I need
to call outside the context I need to fetch by ajax, and calling those
scripts by function calls placed in form onsubmit attributes, for example.
Messy but workable. I'll work on debugging the issue with struts tags later
-- there is no guarantee it's even fixable directly, since I'm using a
snapshot version and not an official distribution.
On Thu, Apr 17, 2008 at 11:19 AM, Jeromy Evans <
jeromy.evans@blueskyminds.com.au> wrote:
> It looks like you're doing everything okay (and well explained) If you
> enable debugging do you get any useful feedback ?
> <s:head theme="ajax" debug="true"/>
>
> The complexity here is that Dojo must parse the response, extract the
> scripts, insert the html in the dom and then execute the scripts. If you
> include an alert in an inline script and it's still not being executed then
> there's definitely a problem with it completing the last operation. You may
> have to switched to the uncompressed dojo script to find out where it's
> failing in within the div widget.
>
> Jukka Välimaa wrote:
>
> > Hi everyone,
> >
> > I'm using Struts 2.1's ajax tags to replace content in a section of my
> > page.
> > I have this section enclosed in sx:div tags, like so:
> >
> > <sx:div id="pageContentSection" preload="false" href="%{myUrl}"
> > listenTopics="/reloadContentSection"
> > afterNotifyTopics="/contentSectionLoaded" executeScripts="true"
> > showLoadingText="true" separateScripts="true"
> > indicator="barIndicator">
> >
> > ..
> >
> > </sx:div>
> >
> > I change the div content by editing its href with javascript and then
> > publishing the topic it's listening to:
> >
> > function reloadContentSection(url){
> > if(!isAnUrl(url)) return false;
> >
> > var contentSection = dojo.widget.byId("pageContentSection");
> > if(contentSection == null) return false;
> >
> > var origHref = contentSection.href;
> >
> > contentSection.href = url;
> >
> > dojo.event.topic.publish('/reloadContentSection', '','');
> >
> > contentSection.href =origHref;
> > }
> >
> > It's working fine otherwise, but I have trouble executing scripts in
> > returned content. I've experimented with different values of
> > executeScripts
> > and separateScripts to no effect. I've even tried to include scripts as
> > a
> > reference to separate javascript library--no better luck there. Even
> > though
> > scripts I write don't work, a datetimepicker in the same returned
> > content,
> > done using ajax tags and of course relying on scripts, works perfectly.
> >
> > When I set separateScripts as false, my scripts are included in
> > generated
> > DOM source, after some scripts by Matt Cruse, which I assume are
> > directly
> > related to struts ajax functionality. Of course, being included doesn't
> > mean
> > that they work.
> >
> > Right now, I've worked around this problem by including the scripts I
> > need
> > in the page I'm calling ajax from, and not in returned content. I don't
> > think this is good design, and won't work if I need to generate scripts
> > dynamically.
> >
> > Can any of you tell me what's wrong, or how to fix it?
> >
> > Jukka
> >
> >
> > ------------------------------------------------------------------------
> >
> > No virus found in this incoming message.
> > Checked by AVG. Version: 7.5.519 / Virus Database: 269.23.0/1379 -
> > Release Date: 15/04/2008 6:10 PM
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Re: Trouble executing scripts in returned ajax content
Posted by Jeromy Evans <je...@blueskyminds.com.au>.
It looks like you're doing everything okay (and well explained) If you
enable debugging do you get any useful feedback ?
<s:head theme="ajax" debug="true"/>
The complexity here is that Dojo must parse the response, extract the
scripts, insert the html in the dom and then execute the scripts. If
you include an alert in an inline script and it's still not being
executed then there's definitely a problem with it completing the last
operation. You may have to switched to the uncompressed dojo script to
find out where it's failing in within the div widget.
Jukka Välimaa wrote:
> Hi everyone,
>
> I'm using Struts 2.1's ajax tags to replace content in a section of my page.
> I have this section enclosed in sx:div tags, like so:
>
> <sx:div id="pageContentSection" preload="false" href="%{myUrl}"
> listenTopics="/reloadContentSection"
> afterNotifyTopics="/contentSectionLoaded" executeScripts="true"
> showLoadingText="true" separateScripts="true" indicator="barIndicator">
>
> ..
>
> </sx:div>
>
> I change the div content by editing its href with javascript and then
> publishing the topic it's listening to:
>
> function reloadContentSection(url){
> if(!isAnUrl(url)) return false;
>
> var contentSection = dojo.widget.byId("pageContentSection");
> if(contentSection == null) return false;
>
> var origHref = contentSection.href;
>
> contentSection.href = url;
>
> dojo.event.topic.publish('/reloadContentSection', '','');
>
> contentSection.href =origHref;
> }
>
> It's working fine otherwise, but I have trouble executing scripts in
> returned content. I've experimented with different values of executeScripts
> and separateScripts to no effect. I've even tried to include scripts as a
> reference to separate javascript library--no better luck there. Even though
> scripts I write don't work, a datetimepicker in the same returned content,
> done using ajax tags and of course relying on scripts, works perfectly.
>
> When I set separateScripts as false, my scripts are included in generated
> DOM source, after some scripts by Matt Cruse, which I assume are directly
> related to struts ajax functionality. Of course, being included doesn't mean
> that they work.
>
> Right now, I've worked around this problem by including the scripts I need
> in the page I'm calling ajax from, and not in returned content. I don't
> think this is good design, and won't work if I need to generate scripts
> dynamically.
>
> Can any of you tell me what's wrong, or how to fix it?
>
> Jukka
>
>
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG.
> Version: 7.5.519 / Virus Database: 269.23.0/1379 - Release Date: 15/04/2008 6:10 PM
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org