Adding onload to body

I’m currently trying to develop a plugin that will embed a Google Earth Tour into a WP post / page via a shortcode.

The issue I am running into is that for the tour to load, I have to add an onload="init()" into the <body> tag.

I can modify a specific template file, but since this is for release, I need to add it dynamically via a hook. Any ideas?

5 s
5

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