【发布时间】:2014-09-12 07:27:30
【问题描述】:
Chrome 的人似乎有自己的 appropriated Element.animate(),使用类似于 onclick="animate();" 的东西有效地破坏了所有现有的 HTML 页面。
这对于 HTML 4/Transitional 规范下的浏览器是否合法?这是 W3C 正式授权的事情吗?
小型网站所有者可以采取什么措施来防止进一步的此类违规行为?
编辑: 测试用例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>test</title>
<script type="text/javascript">
function animate() {
alert('this should work');
}
</script>
</head>
<body>
<input type="button" onclick="animate();" value="push me!">
</body>
</html>
在 Chrome 上的结果为 Uncaught TypeError: Failed to execute 'animate' on 'Element': 1 argument required, but only 0 present.,但在 FireFox 中有效。
【问题讨论】:
-
我敢肯定,没有人能阻止 Google 做任何事情。
-
.animate()方法由 W3C 托管的 currently-in-draft specification 定义(链接在文章的第 3 段)。它不仅适用于 Chrome,而且很有可能被其他浏览器采用。避免冲突的一种选择是 avoid depending on obtrusive/embedded JavaScript 和onevent属性使用的隐含with (this) {}块。 -
我不确定他们是否“挪用”了任何东西。他们为 DOM 中的元素原型添加了一种方法,可能是根据与其他浏览器供应商合作的一些标准草案。我什至不确定您的示例是否受到影响,因为
animate()肯定会寻找window.animate()而新方法将是this.animate() -
正确附加事件监听器,你会没事的。
-
看来我错了,如果设置了
this.animate。但是,您始终可以明确限定函数名称以运行全局函数。请参阅此示例:jsfiddle.net/bKB8E/4
标签: javascript html google-chrome dom chromium