nvcnvn
nvcnvn

Reputation: 5175

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

For example, with a slow internet connection the page might take a few seconds to load.

When a user presses reload or clicks any link on the page, I would like to show a message such as: "The page is loading..."

I can't add the onclick event for all of the links in the page, nor do that with the browser's reload button; is there is even an event for that?

Thanks for your reply!

Upvotes: 1

Views: 93

Answers (2)

psema4
psema4

Reputation: 3047

Add a document.onbeforeunload handler at the top of your <head> section so it executes as soon as possible:

<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>
...

Upvotes: 3

PachinSV
PachinSV

Reputation: 3780

I think you should take a look at a javascript library like jQuery.

Upvotes: 0

Related Questions