Daniel Böhmer Daniel Böhmer - 9 months ago 31
HTML Question

Add event handler for body.onload by javascript within <body> part

We want to include a maps from Google Maps API in our document. The documentation tells to initialize the map with a function called by the onload() event of the body.

The ordinary way to call:

<body onload="initialize_map();">

This doesn't work out for us because we're using Template::Toolkit and the
tag is already included in our wrapper. In short: The
tag is already printed when our javascript code starts running.

I tried something like this but it does only work for
, not
. I guess that's because the javascript code is beneath the
tag itself.

var body = document.getElementsByTagName("body")[0];

body.addEventListener("load", init(), false);

function init() {
alert("it works!");

Any help how to fire up a Google Maps map appreciated!


As we were already using jQuery for a graphical eye-candy feature we ended up using this. A code like

$(document).ready(function() {
    // any code goes here

did everything we wanted and cares about browser incompatibilities at its own.