[英]window.onload works fine with aspx page, but not in regular html
Could someone please explain to me why window.onload
works fine in aspx, but not in xhtml?有人可以向我解释为什么
window.onload
在 aspx 中可以正常工作,但在 xhtml 中不行吗?
This window.onload
example works fine in aspx:这个
window.onload
示例在 aspx 中工作正常:
<script type="text/javascript">
//<![CDATA[
//handles the collapse of submenu items on navigation side menu
function toggle() {
document.getElementById('node4').style.display = '';
}
window.onload = toggle;
//]]>
</script>
Yet this window.onload
example doesn't work in XHTML 1.0 strict.然而这个
window.onload
示例在严格的 XHTML 1.0 中不起作用。 (It doesn't instantly fire countDownClock
when page is loaded ): (加载页面时它不会立即触发
countDownClock
):
<script type="text/javascript">
//<![CDATA[
//handles the collapse of submenu items on navigation side menu
function countDownClock() {
today = new Date();
openingDay = new Date();
openingDay.setMonth(2, 23);
(today > openingDay) ? openingDay.setFullYear(2013) : openingDay.setFullYear();
openingDay.setHours(9, 0, 0, 0);
document.getElementById("mallclock").dayNow.value = showDate(today);
document.getElementById("mallclock").timeNow.value = showTime(today);
var daysLeft = dayDiff(today, openingDay);
var hoursLeft = hoursDiff(today, openingDay);
var minutesLeft = minutesDiff(today, openingDay);
daysLeft = ((hoursLeft - 24) >= 0) ? daysLeft + (hoursLeft / 24) : daysLeft;
hoursLeft = ((hoursLeft - 24) >= 0) ? hoursLeft - ((hoursLeft / 24) * 24) : hoursLeft;
hoursLeft = ((minutesLeft - 60) >= 0) ? hoursLeft + (minutesLeft / 60) : hoursLeft;
minutesLeft = ((minutesLeft - 60) >= 0) ? minutesLeft - ((minutesLeft / 60) * 60) : minutesLeft;
document.getElementById("mallclock").days.value = daysLeft;
document.getElementById("mallclock").hours.value = hoursLeft;
document.getElementById("mallclock").minutes.value = minutesLeft;
}
window.onload = countDownClock;
//]]>
</script>
It only fires the body event, which is set to show countDownClock
1 minute later它只触发 body 事件,该事件设置为 1 分钟后显示
countDownClock
<body onload = "setInterval('countDownClock()', 60000)">
Aha, the edit made all the difference.啊哈,编辑使一切变得不同。
A page can only have one handler for the load
event, so if you add one using window.onload
and add another one using the body onload
attribute, only one of them will work.一个页面只能有一个用于
load
事件的处理程序,因此如果您使用window.onload
添加一个处理程序并使用body onload
属性添加另一个处理程序,则只有其中一个可以工作。
Put both actions in the same handler:将两个操作放在同一个处理程序中:
window.onload = function(){
countDownClock();
window.setInterval(countDownClock, 60000);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.