javascript Chrome - <body onload=""> 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18459305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome - <body onload=""> is not working
提问by Raymond
I know
我知道
"Onload executes when DOM fully loaded.This means it is executed after end of your page. This is useful when you want to do some task when document is fully loaed."
“在 DOM 完全加载时执行 Onload。这意味着它在页面结束后执行。当您想在文档完全加载时执行某些任务时,这很有用。”
but why these code don't work in chrome(spBodyOnLoadWrapper is a function defined in "init.debug.js" , this function was not called ):
但是为什么这些代码在 chrome 中不起作用(spBodyOnLoadWrapper 是在“init.debug.js”中定义的一个函数,这个函数没有被调用):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:o="urn:schemas-microsoft-com:office:office" lang="en-us" dir="ltr">
<head>
<script type="text/javascript">
document.write('<script type="text/javascript" src="/_layouts/1033/init.debug.js?rev=Cn3X2qRiBI8U52EFeStGwg%3D%3D"></' + 'script>');
</script>
</head>
<body scroll="no" onload="if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();" class="v4master">
</body>
These HTML is generated by a Microsoft product named "SharePoint 2010", ugly, and not "best practices" , but i have to make it work in chrome...
这些 HTML 是由名为“SharePoint 2010”的 Microsoft 产品生成的,丑陋,而不是“最佳实践”,但我必须使其在 chrome 中工作......
回答by alex
document.write()
is JavaScript code, so it must be included within a script
element.
document.write()
是 JavaScript 代码,因此它必须包含在script
元素中。
回答by fardjad
I see a few mistakes/bad practices in your HTML:
我在您的 HTML 中看到了一些错误/不良做法:
First of all, you must wrap the document.write
statement in script
tags,
首先,您必须将document.write
语句包装在script
标签中,
Second, using document.write
is not necessary (and should be considered as a bad practice).
You can simply add the script to your page by placing the script
tags in head
or body
:
其次,使用document.write
不是必需的(并且应该被视为一种不好的做法)。您可以通过将script
标签放置在head
or 中来简单地将脚本添加到您的页面body
:
<script type="text/javascript" src="/_layouts/1033/init.debug.jsrev=Cn3X2qRiBI8U52EFeStGwg%3D%3D"></script>
If you want to specify the script source dynamically, you can create a script
element, set its source and add it to your document:
如果要动态指定脚本源,可以创建一个script
元素,设置其源并将其添加到您的文档中:
<script type="text/javascript">
var headElement = document.getElementsByTagName('head')[0],
script = document.createElement('script');
script.setAttribute('src', '/_layouts/1033/init.debug.jsrev=Cn3X2qRiBI8U52EFeStGwg%3D%3D');
script.setAttribute('type', 'text/javascript');
headElement.appendChild(script);
</script>
Instead of using onload
attribute, it's better to add an EventListener for load
event with JavaScript:
与其使用onload
属性,不如load
使用 JavaScript为事件添加一个 EventListener :
window.addEventListener('load', function () {
if (typeof(_spBodyOnLoadWrapper) != 'undefined') {
_spBodyOnLoadWrapper();
}
});
回答by Andy
I ran into this problem and tried the solutions suggested here but they didn't work. Some more searching indicates that this is a bug in Chrome that appears to go back at least to 2009:
我遇到了这个问题并尝试了此处建议的解决方案,但没有奏效。更多的搜索表明这是 Chrome 中的一个错误,至少可以追溯到 2009 年:
http://productforums.google.com/forum/#!topic/chrome/7VIpByhmU3U
http://productforums.google.com/forum/#!topic/chrome/7VIpByhmU3U
The issue is that body.onload (and also window.onload) are firing beforethe web page has completely loaded, apparently in my case because I'm loading large images whose time-to-load varies with net traffic. The work around is to put your JavaScript into the page afterany referenced HTML, but before the end tag:
问题是 body.onload(以及 window.onload)在网页完全加载之前触发,显然在我的情况下是因为我正在加载大图像,其加载时间随网络流量而变化。解决方法是把你的JavaScript到页面后,任何引用的HTML,但最终标记之前:
<script type="text/javascript">
if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();
</script>
This also had the effect for me of producing a substantially more immediate update of the page content in the other browsers (Firefox, Safari) where the bug doesn't occur.
这也对我产生了影响,在其他浏览器(Firefox、Safari)中对页面内容进行了更直接的更新,而这些浏览器不会发生该错误。
回答by Saiyam
check if you have 2 onload events.
检查您是否有 2 个 onload 事件。
回答by Krasimir
I'll suggest to use the following script:
我建议使用以下脚本:
window.onload = function() {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.onreadystatechange = function () {
if(this.readyState == 'complete') {
if (typeof(_spBodyOnLoadWrapper) != 'undefined') {
_spBodyOnLoadWrapper();
}
}
}
script.src = '/_layouts/1033/init.debug.js?rev=Cn3X2qRiBI8U52EFeStGwg%3D%3D';
head.appendChild(script);
}
The usage of document.write is not a good idea. The code above dynamically adds a new script tag into the header of the current page. It detects when this script is loaded and executes your function.
document.write 的用法不是一个好主意。上面的代码动态地将一个新的脚本标签添加到当前页面的页眉中。它检测何时加载此脚本并执行您的函数。
回答by Jeroen Ingelbrecht
Just put the script tags in the head without document.write
. Secondly, I suggest you put the code which you want to execute in a separate function as well in your head section but anywho, below example might work as well (see also JS Bin):
只需将脚本标签放在没有document.write
. 其次,我建议你把你想要执行的代码放在一个单独的函数中,也放在你的 head 部分,但是任何人,下面的例子也可以工作(另见JS Bin):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:o="urn:schemas-microsoft-com:office:office" lang="en-us" dir="ltr">
<head>
<script type="text/javascript" src="/_layouts/1033/init.debug.js?rev=Cn3X2qRiBI8U52EFeStGwg%3D%3D"></script>
</head>
<body scroll="no" onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined'){ _spBodyOnLoadWrapper();}" class="v4master">
</body>