在< body>内通过javascript为body.onload添加事件处理程序部分 [英] Add event handler for body.onload by javascript within <body> part

查看:98
本文介绍了在< body>内通过javascript为body.onload添加事件处理程序部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望在我们的文档中包含来自Google Maps API的地图。这个文档告诉我们用一个由body的onload()事件调用的函数来初始化map。

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.

调用方式:

<body onload="initialize_map();">

这对我们来说并不奏效,因为我们使用Template :: Toolkit和< body> 标签已经包含在我们的包装中。简而言之:当我们的javascript代码开始运行时,已经打印了< body> 标记。

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

像这样,但它只适用于 onclick ,而不是 onload 。我想这是因为JavaScript代码位于< body> 标签本身之下。

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

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

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

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

任何帮助如何启动Google Maps地图的人都会感激!

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

推荐答案

由于我们已经使用jQuery来实现图形视觉特效,因此我们最终使用了这个功能。类似于

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
    init();
});

完成了我们想要的任何事情,并关心自己的浏览器不兼容问题。

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

这篇关于在&lt; body&gt;内通过javascript为body.onload添加事件处理程序部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆