Baconbutty wrote:
I suppose "eval" might work.
There is also a clue in RobG's answer.
Setting the text attribute of the script element does indeed add and run
the script as the OP requires - my test had a typo in the script that
caused it to fail (dang browser thinks 'javascript' and 'javscript' are
two different things).
var oScript=documen t.createElement ("script");
oScript.text=[[variable holding responseText]];
oScript.defer=" true";
document.getEle mentsById("head ")[0].appendChild(oS cript);
The defer attribute does not necessarily affect when the script is run,
it affects what the browser does while the script is loading and
executing. I would expect the browser to load the entire script before
executing any part of it.
A test is to have a statement at the very start of a script file that
calls a function at the very bottom of the file with a large amount of
guff in between - I've never seen this fail (maybe I just haven't used a
large enough file or slow enough connection) - though in practice I
ensure all functions are loaded first).
My reading of the HTML spec is that 'defer' tells the UA that it can
load the script element content asynchronously because it doesn't
generate any document content. In other words, don't defer
parsing/rendering the rest of the document until the script is loaded
(which is kinda contrary to what you might expect 'defer=true' to mean).
"When set, this boolean attribute provides a hint to the user agent
that the script is not going to generate any document content (e.g.,
no "document.write " in javascript) and thus, the user agent can
continue parsing and rendering."
<URL:http://www.w3.org/TR/html4/interact/scripts.html#ad ef-defer>
And in the index of attributes it says:
"UA may defer execution of script"
<URL:http://www.w3.org/TR/html4/index/attributes.html >
The second reference seems to contradict the first somewhat, maybe the
words 'while the rest of the document loads' should be added.
'defer' is probably only of use if the script is in the body of the
document or loaded from an external file and there is something else for
the UA to get on with while it loads and executes.
--
Rob