[Greasemonkey] Adding a script to the document.head?
chris olive
perljunkie at gmail.com
Thu Sep 8 15:52:40 EDT 2005
2005/9/8, Aaron Boodman <zboogs at gmail.com>:
> 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.
Dude... Love your answer. This is much better and the direction you
are going with this can easily be applied to other problems. This is
a better way to go. The innerHTML method is clearly "hackish" in
light of this. Thanks.
-pj
More information about the Greasemonkey
mailing list