In struts 2 we already do that for you(link tag and button tag in the
ajax theme), this is the code to extract the javascript sections(from Dojo):
parse : function(s) {
this.log("Parsing: " + s);
var match = [];
var tmp = [];
var scripts = [];
while(match){
match = s.match(/<script([^>]*)>([\s\S]*?)<\/script>/i);
if(!match){ break; }
if(match[1]){
attr = match[1].match(/src=(['"]?)([^"']*)\1/i);
if(attr){
// remove a dojo.js or dojo.js.uncompressed.js from remoteScripts
// we declare all files with dojo.js as bad, regardless of folder
var tmp2 = attr[2].search(/.*(\bdojo\b(?:\.uncompressed)?\.js)$/);
if(tmp2 > -1){
this.log("Security note! inhibit:"+attr[2]+" from beeing
loaded again.");
}
}
}
if(match[2]){
// strip out all djConfig variables from script tags nodeValue
// this is ABSOLUTLY needed as reinitialize djConfig after dojo
is initialised
// makes a dissaster greater than Titanic, update remove
writeIncludes() to
var sc = match[2].replace(/(?:var
)?\bdjConfig\b(?:[\s]*=[\s]*\{[^}]+\}|\.[\w]*[\s]*=[\s]*[^;\n]*)?;?|dojo\.hostenv\.writeIncludes\(\s*\);?/g,
"");
if(!sc){ continue; }
// cut out all dojo.require (...) calls, if we have execute
// scripts false widgets dont get there require calls
// does suck out possible widgetpackage registration as well
tmp = [];
while(tmp){
tmp =
sc.match(/dojo\.(?:(?:require(?:After)?(?:If)?)|(?:widget\.(?:manager\.)?registerWidgetPackage)|(?:(?:hostenv\.)?setModulePrefix))\((['"]).*?\1\)\s*;?/);
if(!tmp){ break;}
sc = sc.replace(tmp[0], "");
}
scripts.push(sc);
}
s = s.replace(/<script[^>]*>[\s\S]*?<\/script>/i, "");
}
return {
text: s,
scripts: scripts
};
}
and to execute it:
var parsed = this.parse(data);
//eval scripts
if(parsed.scripts && parsed.scripts.length > 0) {
var scripts = "";
for(var i = 0; i < parsed.scripts.length; i++){
scripts += parsed.scripts[i];
}
(new Function('_container_', scripts+'; return this;'))(this);
}
where 'data' is the html returned from the action.
have fun :)
musachy
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]