Adding onload to body

And here’s a jQuery solution (as Mike suggested in his first comment).

function add_my_scripts() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'my_init_script', SCRIPTSRC, 'jquery', '1.0' );
}
add_action( 'init', 'add_my_scripts' );

Then add a script to your plug-in that does this:

jQuery.noConflict();

jQuery(document).ready(function($) {
    init();
});

This will start jQuery in no conflict mode (if it isn’t already) and add a call to the init() method when the document is ready. It’s a safer method to use than body onready() because the onready() function can only call one thing … so no one else can hook anything to that or add custom script. It’s better to make your plug-in as unobtrusive as possible so other plug-ins don’t interfere or vice-versa.

Leave a Comment