JavaScript 窗口加载函数调用
本文通过一个例子来解释 JavaScript 中 window.onload 方法的用途。一旦浏览器上的页面完成加载,window.onload 函数就会执行以执行任务。
window.onload
功能用法与 HTML 中的示例
window.onload
方法在页面完全加载到浏览器(以及所有图像、样式、脚本和 CDN)时触发。在这里,你可以看到一个 HTML
页面源代码示例,该示例使用 window.onload 方法自动执行以显示警告框。
<html>
<head>
<title>(Window.onload function in HTML)</title>
<script type="text/javascript">
function code(){
alert(" Alert inside code function Window.onload");
}
window.onload=code();
</script>
</head>
<body>
<h1> Graphic Designer Firm </h1>
<p1> A craft of creating visual content to communicate messages.</p1>
</br> <img src = "Graphic-Design.png" height="500px" width="500px" >
</br> <hr width="800">
</body>
</html>
在上面的 HTML
页面源代码中,你可以看到在 doctype html 中使用 JavaScript
的 <script type="text/javascript">
标记。在下一步中,在 <script>
标签内声明了 function code()
。
function code()
被称为 window.onload
。当网页完全加载图像和文本内容时,它会显示默认警报对话框。
用几个步骤检查给定的 HTML
代码
按照这四个简单的步骤,可以清楚地了解 window.onload
方法。
-
创建一个文本文档。你可以使用记事本或任何其他文本编辑工具。
-
将给定的代码粘贴到创建的文本文件中。
-
使用
.html
扩展名保存该文本文件,并使用任何默认浏览器打开它。 -
你可以看到在完整页面加载后立即弹出的警告框。
查看相同结果的替代方法
你可以通过 body 标签使用替代的 onload
事件来获得警报消息结果。下面是一个使用自动执行来调用包含警告框的函数代码的 body 标签示例:
<script type="text/javascript">
function code(){
alert(" Alert inside code function");
}
</script>
<body onLoad="code()";>
在这两个示例中,body 标签的 onLoad
和 window.onload
具有相同的功能,可以在完成页面加载后执行语句。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。