• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

[JS] Add Onload to <BODY> Tag

doof92

New Member
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:
Code:
<body class="oneColFixCtr">

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

Thanks very much
 
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 -_-
 
Last edited:
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..

Code:
window.onload=function() {
  //do this...
    perhapsanotherFunction() {
      //...
  }
}
 
You can't do that manually with greasemonkey

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

Code:
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.
 
No, I mean you can't put things in manually with greasemonkey. Everything has to be controlled with JS.
 
So with Greasemonkey, is it possible to just do:
Code:
window.onload=function() {
document.forms['myForm'].elements['firstField'].focus()
}
 
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 ;)
 
Back
Top