首页 > 解决方案 > 即使我将脚本标记放在正文标记的结尾上方,页面也仅在单击弹出窗口后才加载

问题描述

在此处输入图像描述

即使我将我的脚本标签放在正文标签的结尾上方,页面也只有在单击确定后才会加载页面。我该怎么办?

<!DOCTYPE html> 
 <html lang="en" dir="ltr">  
   <head>   
    <meta charset="utf-8">  
    <title></title>
   </head> 
   <body>    
    <h1>this is for beginners javascript</h1>   
    <div>  <p>this is a div</p>   </div> 
    <script src="js_notes.js"></script>  
   </body>  
 </html>

js_notes.js 仅包含下面给出的这一行

alert('hello world');

标签: javascript

解决方案


你想让我做什么 ?加载 html 时将始终读取 js 文件。如果您只想在执行事件时加载弹出窗口(例如单击 div :这是一个 div),只需使用 onclick 函数。

在您的 js_notes.js 中:

function showpopup() {
  alert('hello');
}

如果您只想等待页面加载,可以使用 window.onload 或计时器 (setTimeout(showpopup, 1000))

在您的 html 文件中:

<div onclick='showpopup()'>Text Content Here</div>

推荐阅读