[Greasemonkey] Adding a script to the document.head?
Aaron Boodman
zboogs at gmail.com
Thu Sep 8 11:54:46 EDT 2005
On 9/8/05, chris olive <perljunkie at gmail.com> wrote:
> var strSomeHTML = ''
> + '<script language="javascript">'
> + 'function dude() { alert( "Dude!" ); }'
> + '</script>'
> + '<form>'
> + '<input type="button" name="btnDude" value="Say Dude" onClick="dude();">'
> + '</form>';
*shiver*
Don't do that. I'm not even going to tell you how to make that work :-).
You can create the form using dom methods:
var f = document.createElement("form");
var b = document.createElement("input");
b.setAttribute("type", "button");
... etc ...
f.appendChild(b);
document.body.appendChild(f);
And you can hook up event handlers like this:
function sayDude() {
alert("dude!");
}
b.addEventListener("click", sayDude, false);
It's a little more verbose but it's way easier to read and maintain.
Once your script becomes even a teeny bit complicated, you'll thank
me.
Try to forget you know about innerHTML and the on* attributes. They
will only bring you (and I) pain.
--
Aaron
More information about the Greasemonkey
mailing list