PDA

View Full Version : [JS] Add Onload to <BODY> Tag



doof92
June 25th, 2008, 13:30
Hey,

I want to, using Javascript, to add an onload script to the <body> tag. IT will be a Greasemonkey script, and using Javascript I want to do the following:

Say I have a body tag of:
<body class="oneColFixCtr">

I want to change this, using Javascript, to:
<body class="oneColFixCtr" onload="document.forms['myForm'].elements['firstField'].focus()">

Thanks very much

DarkBlood
July 4th, 2008, 00:57
Why not just put the onLoad in there manually? :S

DOM can't attach an onLoad event since onLoad has already passed. My getElementsByClassName can't do this either -_-

themoose
July 4th, 2008, 15:16
Why not just put the onLoad in there manually? :S

DOM can't attach an onLoad event since onLoad has already passed. My getElementsByClassName can't do this either -_-

You can't do that manually with greasemonkey.

I think you want something like this doof92..



window.onload=function() {
//do this...
perhapsanotherFunction() {
//...
}
}

DarkBlood
July 4th, 2008, 16:25
You can't do that manually with greasemonkey

getElementsByClassName isn't just a greasemonkey, it's valid JS:


function getElementsByClassName(classname, node) {
if(!node) node = document.getElementsByTagName("body")[0];
var a = [];
var re = new RegExp('\\b' + classname + '\\b');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}

It's just bulky since you have to add it manually.

themoose
July 5th, 2008, 05:05
No, I mean you can't put things in manually with greasemonkey. Everything has to be controlled with JS.

doof92
July 5th, 2008, 05:21
So with Greasemonkey, is it possible to just do:


window.onload=function() {
document.forms['myForm'].elements['firstField'].focus()
}

themoose
July 5th, 2008, 05:23
I would presume so! Never actually used greasemonkey (for developing) but I fully understand the concept behind it.

Best way is to test it out ;)