HTML - attributes - onpageshow.

revision:


Content

"onpageshow" attribute : when navigating to a webpage syntax some examples


"onpageshow" attribute : when navigating to a webpage

top

The onpageshow event occurs when a user navigates to a webpage.

Supported HTML tags:<body>

The onpageshow event is similar to the "onload event", except that it occurs after the onload event when the page first loads. The onpageshow event occurs every time the page is loaded, whereas the onload event does not occur when the page is loaded from the cache.


syntax

top

<element onpageshow="script"></element>

script: the script to be run on onpageshow.


some examples

top

example 1

                <p class="spec">This example uses the HTML DOM to assign an "onpageshow" event to a body element.</p>
                <h3 style="margin-left:3vw;" id="demo"></h3>
                <script>
                    document.getElementsByTagName("BODY")[0].onpageshow = function() {myFunction()};
                    
                    function myFunction() {
                      document.getElementById("demo").innerHTML = "Welcome To My Homepage!";
                    };
                    
                    /* This is equivalent to assigning the event to the window object:
                    window.onpageshow = function() {
                      document.getElementById("demo").innerHTML = "Welcome To My Homepage!";
                    };
                    */
                </script>
            

example 2

                <body onpageshow="myPage()">
                    <h1 style="color:green;text-align:center;">My first webpage                                                                                     
                    <script>
                        function myPage() {
                            alert("Welcome to GeeksForGeeks!");
                        }
                    </script>
                </body>