animate
is used both as a function name and an id, which cause it to not work in Chrome.
Also, mentioned in this post, a function alone with the name animate
might also not work in Chrome, based on how it is implemented. (see notes below)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
var frame = 2;
function animatee(){
if(frame == 1){
frame = frame + 1;
document.getElementById("animate").src = "http://placehold.it/100";
}
else if (frame == 2){
frame = frame + 1;
document.getElementById("animate").src = "http://placehold.it/100/f00";
}
else{
frame = 1;
document.getElementById("animate").src = "http://placehold.it/100/00f";
}
}
</script>
</head>
<body>
<p> clicking the button will change the image.</p>
<img id="animate" src="http://placehold.it/100/0f0">
<button onclick="animatee();">click me to animate</button>
</body>
</html>
Notes:
An observation made by guest271314 shows that if the onclick handler is not attached inline, the issue is not present
window.onload = function() {
var frame = 2;
function animate() {
console.log("animate called")
if (frame == 1) {
frame = frame + 1;
document.getElementById("animatee").src = "http://placehold.it/100x100";
} else if (frame == 2) {
frame = frame + 1;
document.getElementById("animatee").src = "http://placehold.it/200x200";
} else {
frame = 1;
document.getElementById("animatee").src = "http://placehold.it/100/00f";
}
}
document.querySelector("button").onclick = animate;
}
<p> clicking the button will change the image.</p>
<img id="animatee" src="http://placehold.it/100/0f0">
<button>click me to animate</button>
To their defense Kaiido contributed with the specs: https://html.spec.whatwg.org/multipage/nav-history-apis.html#named-access-on-the-window-object
Knu contributed with this bug report:https://www.w3.org/Bugs/Public/show_bug.cgi?id=11960