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>

Leave a comment

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

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started