当用户在HTML页面上导航时执行脚本?

WBOY
发布: 2023-08-20 13:13:06
转载
978人浏览过

当用户在html页面上导航时执行脚本?

The task we need to perform in this article is executing a script when a user navigates to a page in HTML.

We can do the above task (executing a script when a user navigates to a page in HTML) by using "onpageshow Event". Before we jump into the examples let’s look into the definition and usage of onpageshow event in HTML.

HTML onpageshow事件

The onpageshow event in HTML occurs when a user navigates to a webpage. This event occurs every time the page is loaded.

语法

以下是HTML中onpageshow事件的语法 -

立即学习前端免费学习笔记(深入)”;

<element onpageshow = "myScript">
登录后复制

以下是我们在HTML中使用onpageshow事件的示例,当用户在HTML中导航到一个页面时。

Example 1

In the example,

  • 当用户在HTML中导航到一个页面时,我们使用了onpageshow事件。

  • 我们在函数内部编写了一个alert()方法。因此,每当用户尝试导航到一个页面时,onpageshow事件将被触发。

<!DOCTYPE html>
<html>
<head>
   <title>Execute a script when a user navigates to a page in HTML?</title>
</head>
<body onpageshow="navigate()">
   <h2>Execute a script when a user navigates to a page in HTML?</h2>
   <p><b>Note:</b> The <strong>"onpageshow"</strong> event in HTML will not supported in Internet Explorer 10 and before versions.</p>
   <script>
      function navigate() {
         alert("Welcome to the page!");
      }
   </script>
</body>
</html>
登录后复制

As we can see in the output; when the user tries to navigate to a page an alert will be displayed on the window.

麦当秀MindShow AiPPT
麦当秀MindShow AiPPT

麦当秀|MINDSHOW是一款百万用户正在使用的三分钟生成一份PPT的AI应用系统。它利用引领前沿的人工智能技术,能够自动完成演示内容的设计。

麦当秀MindShow AiPPT 224
查看详情 麦当秀MindShow AiPPT

Example 2

的中文翻译为:

示例2

In the example below,

  • 当用户在HTML中导航到一个页面时,我们使用了onpageshow事件。

  • 我们在函数内部编写了一个打印语句。因此,每当用户尝试导航到一个页面时,onpageshow事件将被触发。

<!DOCTYPE html>
<html>
<head>
   <title>Execute a script when a user navigates to a page in HTML?</title>
</head>
<body onpageshow="navigate()">
   <h2>Execute a script when a user navigates to a page in HTML?</h2>
   <p><b>Note:</b> The <strong>"onpageshow"</strong> event in HTML will not supported in Internet Explorer 10 and before versions.</p>
   <h2 id="heading"> </h2>
   <script>
      function navigate() {
         document.getElementById("heading").innerHTML = "Welcome to the page!";
      }
   </script>
</body>
</html>
登录后复制

As we can see in the output when the user tries to navigate to a page the print statement will be executed.

Example 3

的中文翻译为:

示例3

In the example below,

  • 当用户在HTML中导航到一个页面时,我们使用了onpageshow事件。

  • 我们在函数内部编写了一个打印语句。因此,每当用户尝试导航到一个页面时,onpageshow事件将被触发。

  • In the below code; we have written "window.onpageshow", here window interface represents a window containing a DOM document.

<!DOCTYPE html>
<html>
<head>
   <title>Execute a script when a user navigates to a page in HTML?</title>
</head>
<body>
   <h2>Execute a script when a user navigates to a page in HTML?</h2>
   <h3 id="para"></h3>
   <script>
     function Navigate() {
       document.getElementById("para").innerHTML = "Welcome to the page!";
     }
     window.onpageshow = Navigate;
   </script>
</body>
</html>
登录后复制

As we can see in the output when the user tries to navigate to a page the print statement will be executed.

以上就是当用户在HTML页面上导航时执行脚本?的详细内容,更多请关注php中文网其它相关文章!

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号