html加载之前执行警报 [英] alert is executing before html loads

查看:76
本文介绍了html加载之前执行警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的问题是有关如何加载网页的!这是我的代码:

Hello my questions is about how a webpage is loaded! Here is my code:

<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title> 
</head>
<body>
<h1>Waiting</h1>
<script type="text/javascript">
    alert("Why?");
</script>
</body>
</html>

我无法终生显示标题前为何运行警报.我的理解是,由于警报就在关闭的正文标签上方,因此它将是最后运行的事情.为什么页面在显示标题之前等待我关闭警报?

I cannot for the life of me figure out why the alert is running before the heading is displayed. It is my understanding that since the alert is right above the closing body tag it will be the last thing run. Why is the page waiting for me to close out the alert before displaying the heading?

感谢您的帮助!

我在firefox中而不是在chrome中运行此代码,并且按我希望的方式工作-警报运行前首先显示标题.

I ran this code in firefox rather than chrome and it worked how I wanted it to - the heading displayed first before the alert ran.

推荐答案

在页面加载

<body onload="script();">

将在页面加载之前执行外部脚本.

An external script will execute before the page loads.

<body onload="script();">

<h1>Waiting</h1>
<script type="text/javascript">
   function script() {alert("Why?");}
</script>
</body>

这篇关于html加载之前执行警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆