.. wrote:
This is the exact question I have. Unfortunately, perusing
the billion messages this list generates, it's been asked
several times in the past, with no answers. So I'm guessing:
no.
<snip>
While it may not be possible to insert the code imported by an external
script in a function so that its execution can be deferred, it would be
possible to defer any document.write calls that such code may use.
First copy the real document.write mehtod to another property of the
document object (to preserve its - this - reference when executed):-
document.oldWrite = document.write;
Then create an array in a suitable containing scope (global here) to
contain anything written:-
var stuffThatWasWritten = [];
Then replace the document.write method with one that will just copy its
arguments to that array:-
document.write = function(){
for(var c = 0;c < arguments.length;c++){
stuffThatWasWritten[stuffThatWasWritten.length] = arguments[c];
}
}
Now import the external script and instead of writing to the document it
will just store strings in the array.
Then resort the document.write method (or use the copy of it) and write
out whatever is in the array at any point you choose:-
document.oldWrite(stuffThatWasWritten.join(''));
-or:-
document.write = document.oldWrite;
document.write(stuffThatWasWritten.join(''));
(but don't forget the document.writeln method)
The closest I can come is something like::
document.write("<script src='stupid.js'><" + "/script>");
/* NOTE the break is need to prevent
it from closing your script prematurely.*/
The concatenation is not needed. Only disrupting the '</' character
sequence in the source code is required and can be achieved by escaping
the forward slash, resulting in '<\/'. And avoiding the concatenation is
fractionally more efficient.
Richard.