Skip to content
Advertisements

JavaScript onpageshow Event

Why onpageshow ?

The JavaScript onpageshow property occurs when the page is loaded. But not immediately. onload occurs immediately after the page is loaded. onpageshow takes place after the onload feature (another difference is the time to load in the cache).

<element onpageshow=”script”>

  • script: Codes to load when loading a web page.

JavaScript onpageshow Examples

Example 1

onpageshow example 1
<body onpageshow="onPageShowFunction()">

  <h2>truecodes.org</h2>
  <p>onpageshow experiment</p>

    <script>
    
        function onPageShowFunction() {
            alert("onpageshow worked successfully")
        }

    </script>
</body>

Example 2

onpageshow example 2
  window.addEventListener("pageshow", onPageShow);

        function onPageShow() {
            alert("onpageshow worked successfully")
        }

Example 3

onpageshow example 3
<body onload="onloadFunction()" onpageshow="onpageFunction()">

    <h2>onload VS onpageshow</h2>

    <script>

        function onloadFunction() {
            alert("onloadFunction")
        }

        function onpageFunction() {
            alert("onpageFunction")
        }


    </script>
</body>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: