That will only work if the response is nothing but JavaScript, in which
case your 100% correct (although many people say that eval() should be
renamed evil() and should be avoided like the plague... I'm not *quite*
that extreme in my avoidance of it).
As a quick proof:
<html>
<head>
<script>
var s = "";
// s += "<" + "html" + ">";
// s += "<" + "head" + ">";
// s += "<" + "script" + ">";
s += "alert('test');";
// s += "<" + "/script" + ">";
// s += "<" + "/head" + ">";
// s += "<" + "body" + ">";
// s += "Hello";
// s += "<" + "/body" + ">";
// s += "<" + "/html" + ">";
function testit() {
eval(s);
}
</script>
</head>
<body>
The Body
<input type="button" value="testit" onclick="testit();">
</body>
</html>
Load this file in your browser and you'll find that you get an alert, as
expected... now, uncomment the commented lines and reload and you'll see
that it no longer pops the alert (interestingly, in IE I get a syntax
error, because it's trying to interpret the markup as script, but in FF
it just quietly doesn't work, not even a notice in Firebug).
So, if the idea is to execute script blocks that are part of an HTML
response, a simple eval() won't work... but if all your returning is
script, then I definitely echo Chris' suggestion and would go with
eval() (regardless of who wants to slap your hand with a ruler).
Frank
Chris Pratt wrote:
Or you could just call eval(ajax.responseText).
(*Chris*)
On 11/28/06, Frank W. Zammetti <[EMAIL PROTECTED]> wrote:
Hi Adam,
If your doing straight AJAX yourself, i.e., directly interacting with the
XMLHttpRequest object, this won't execute script for you automatically.
In fact, it won't do much of anything for you automatially, aside from
parsing XML if that's your return type. Otherwise, it's just text to the
object and you'll have to execute scripts yourself.
Another poster gave you some info if your using S2, but I'm guessing by
your description your using S1. In that case, continue reading! :)
The AjaxParts Taglib (APT) in Java Web Parts (JWP) takes care of this for
you... you can certainly switch over to using APT, but if you just want
some code to execute scripts, take a look here:
http://javawebparts.cvs.sourceforge.net/javawebparts/javawebparts/WEB-INF/src/javawebparts/ajaxparts/taglib/resources/AjaxPartsTaglib.js?view=markup
Down around line 313 you'll find the execScripts() function... simply
yank
that out and use it on the responseText from XMLHttpRequest and you'll be
good to go.
If you are interested in looking at APT, here's a link:
http://javawebparts.sourceforge.net/javadocs/javawebparts/ajaxparts/taglib/package-summary.html
And for JWP in general:
http://javawebparts.sourceforge.net
Hth,
Frank
--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: [EMAIL PROTECTED]
Author of "Practical Ajax Projects With Java Technology"
(2006, Apress, ISBN 1-59059-695-1)
Java Web Parts - http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!
On Tue, November 28, 2006 3:43 pm, Adam Gordon wrote:
> I have a JSP and there's a link in the rendered page that makes an AJAX
> call
> (to a Struts action) when clicked. The results of that action, and the
> contents of the response are set as the innerHTML on a hidden <div>
> defined
> inside the afore mentioned rendered page. The <div> is then un-hid.
>
> Everything is working correctly except for one part: When the div is
> displayed, the JavaScript code in the contents returned by the AJAX
call
> isn't being executed and thus, the contents of the <div> aren't set up
> correctly.
>
> Does anyone know a way to have the JavaScript be executed? Or force
the
> browser to execute it?
>
> I used to have an <iframe> and everything worked great, but there was a
> bad
> side effect with session timeouts and so we've decided to not use them.
>
> Any help would be appreciated. Thanks.
>
> -Adam
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: [EMAIL PROTECTED]
Author of "Practical Ajax Projects With Java Technology"
(2006, Apress, ISBN 1-59059-695-1)
Java Web Parts - http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]