简体   繁体   English

在HTML末尾执行JavaScript函数而不使用body onload有什么问题?

[英]What is wrong with executing the JavaScript function at the end of the HTML instead of using body onload?

This is about calling the initialize function for the Google Maps API. 这是关于调用Google Maps API的initialize函数。 I would prefer to call it at the end of the HTML instead of putting it in the body onload . 我宁愿在HTML的末尾调用它,而不是将其放在正文onload中 It appears to be working, but is there anything wrong with this approach? 它似乎正在工作,但是这种方法有什么问题吗?

The Google API code I am referring to can be viewed at Place Autocomplete Address Form . 我所指的Google API代码可以在“ 地点自动填充地址表格”中查看。


This is not a duplicate of window.onload vs <body onload=""/> . 这不是window.onload与<body onload =“” />的重复。

Or this closed one, Unobtrusive JavaScript onload function . 或关闭的, 不引人注目的JavaScript onload函数

It is closest to this one: Initializing JS components at the end of HTML or on "onload"? 最接近这一点:是在HTML末尾还是在“ onload”上初始化JS组件?

However, my question is not generic. 但是,我的问题不是通用的。

That's just personal preference. 那只是个人喜好。 Both ways work fine. 两种方式都可以正常工作。 They probably used the onload handler because it appears to be more foolproof. 他们可能使用了onload处理程序,因为它似乎更加安全。

If the code was at the end of the file, some people might copy it and happily edit around without fully understanding why it is written at the end of the file and then later on shoot out a bunch of posts asking why their code isn't working. 如果代码位于文件的末尾,则有些人可能会复制它并愉快地进行编辑,而不必完全理解为什么在文件末尾写了它,然后后来出了一堆帖子问他们的代码为什么不工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM