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

当用户在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事件的语法 –

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


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

Example 1

In the example,

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

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

   Execute a script when a user navigates to a page in HTML?   

Execute a script when a user navigates to a page in HTML?

Note: The "onpageshow" event in HTML will not supported in Internet Explorer 10 and before versions.

function navigate() { alert("Welcome to the page!"); }

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

Example 2

的中文翻译为:

示例2

In the example below,

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

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

   Execute a script when a user navigates to a page in HTML?   

Execute a script when a user navigates to a page in HTML?

Note: The "onpageshow" event in HTML will not supported in Internet Explorer 10 and before versions.

function navigate() { document.getElementById("heading").innerHTML = "Welcome to the page!"; }

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.

   Execute a script when a user navigates to a page in HTML?   

Execute a script when a user navigates to a page in HTML?

function Navigate() { document.getElementById("para").innerHTML = "Welcome to the page!"; } window.onpageshow = Navigate;

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1551057.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月21日 21:16:36
下一篇 2025年12月21日 21:16:58

发表回复

登录后才能评论
关注微信