当有人重新加载页面或单击链接时,我如何捕捉JavaScript事件

How can I capture the JavaScript events when someone reloads the page or clicks on a link?

本文关键字:链接 单击 事件 JavaScript 何捕捉 新加载 加载      更新时间:2023-09-26

例如,如果互联网连接速度较慢,页面可能需要几秒钟才能加载。

当用户按下重载或点击页面上的任何链接时,我想显示一条消息,例如:"页面正在加载…"

我不能为页面中的所有链接添加onclick事件,也不能使用浏览器的重新加载按钮来添加;有这样的活动吗?

谢谢你的回复!

<head>部分的顶部添加一个document.onbeforeunload处理程序,以便它尽快执行:

<head>
    <script>
        window.onbeforeunload = function(e) {
          return "The page is loading... Are You Sure?"
        };
        window.onload = function(e) {
            document.getElementsByTagName('title')[0].innerHTML = 'Loaded';
            window.onbeforeunload = function() { };
        }
    </script>
    <title>Loading</title>
    ...
</head>
...

我认为您应该看看像jQuery这样的javascript库。