JavaScript页面加载后自动执行
JavaScript和jQuery中关于页面加载完成自动执行有以下几种写法:
//第一种
window.onload = function(){};
//第二种
$(document).ready(function(){});
$(function(){});
//第三种
$(window).ready(function(){});
//第四种 <body onload="alertXXX('body')">
//第五种
document.readyState = function(){};
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>页面加载</title>
<script src="jQuery-3.4.1.js"></script>
<script>
function alertXXX(str) {
alert('This is————' + str)
}
$(window).ready(alertXXX('$(window).ready()'));
window.onload = alertXXX('window.onload');
document.readyState = alertXXX('document');
$(document).ready(alertXXX('$(document).ready()'));
$().ready(alertXXX('$().ready()'));
</script>
</head>
<body onload="alertXXX('body')">
</body>
</html>