onload
当页面载入完毕后执行Javascript代码。
静态注册,
只执行一句JavaScript代码:只在body的标签内写 onload
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body onload="alert('静态注册,只执行一句代码,可以在这里写')">
</body>
</html>
静态注册,
可能执行多句JavaScript代码:先在head标签中写js代码函数,然后在body标签中调用这个在head标签中定义的js代码函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript">
alert('静态注册,执行多句代码,以这种函数的形式');
</script>
</head>
<body onload="onLoadFun();">
</body>
</html>
动态注册
直接在head标签的js代码中写:window.onload()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript">
window.onload = function(){
alert("动态注册的onload事件");
}
</script>
</head>
<body>
</body>
</html>