[Greasemonkey] Adding a javascript file?
Dave Land
land at aol.com
Thu May 25 12:25:13 EDT 2006
On May 24, 2006, at 7:58 PM, Stefan Zochowski wrote:
> Are we supposed to be able to do something along the lines of:
>
> document.body.innerHTML=document.body.innerHTML+'xx<b>x</b> <SCRIPT
> language="JavaScript"> \nalert("worked!");\n </script>xxxx';
That's certainly one way to add a script to a page (appending text
to the innerHTML of the body), but there's a better one that takes
advantage of JavaScript's access to the DOM model... Don't be
daunted by the fact that it's many times longer than yours.
// Set up text nodes
singleX = document.createTextNode("x");
doubleX = document.createTextNode("xx");
quadroX = document.createTextNode("xxxx");
bolderX = document.createElement("b");
bolderX.appendChild(singleX);
// Set up script node
scripTx = document.createTextNode(" \nalert("worked!");\n");
jScript = document.createElement("script");
jScript.setAttribute("type","text/javascript");
jScript.appendChild(scripTx);
// Attach new nodes to document
document.body.appendChild(doubleX);
document.body.appendChild(bolderX);
document.body.appendChild(jScript);
document.body.appendChild(quadroX);
The basic pattern for adding a given element is:
- construct the object you want to add
(may take multiple steps, as in the case of the script)
- append it to a parent object such as the body
Dave
More information about the Greasemonkey
mailing list