{"id":59794,"date":"2023-07-06T03:01:27","date_gmt":"2023-07-05T17:01:27","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=59794"},"modified":"2023-07-06T07:13:06","modified_gmt":"2023-07-05T21:13:06","slug":"onpageshow-and-onpagehide-and-onvisibilitychange-events-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/onpageshow-and-onpagehide-and-onvisibilitychange-events-primer-tutorial\/","title":{"rendered":"Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/pageTransitions.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/onpageshow_and_onpagehide_and_onvisibilitychange_events.gif\" title=\"Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial\"  style=\"float:left;\"  \/><\/a><p class=\"wp-caption-text\">Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial<\/p><\/div>\n<p>Today, we discuss alternative event ideas to document.body <i>onload<\/i> and <i>onunload<\/i> events tracing the navigating to and from a webpage, respectively.<\/p>\n<p>The three event types we&#8217;ve experimented with, today, in our proof of concept HTML and Javascript <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/pageTransitions.html_GETME\" title=\"pageTransitions.html\">pageTransitions.html<\/a>, are, with mixed success &#8230;<\/p>\n<ol>\n<li><a target=_blank title='Onpageshow event' href='https:\/\/www.w3schools.com\/jsref\/event_onpageshow.asp'>onpageshow<\/a> (at same time as document.body onload event) &#8230; opening &#8230; versus &#8230; closing &#8230;<\/li>\n<li><a target=_blank title='Onpagehide event' href='https:\/\/www.w3schools.com\/jsref\/event_onpagehide.asp'>onpagehide<\/a> (we had little success with this event) &#8230; and we had more success with &#8230;<\/li>\n<li><a target=_blank title='Onvisibilitychanged event' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/visibilitychange_event'>onvisibilitychange<\/a> (in conjunction with <i>document.visibilityState === &#8220;hidden&#8221;<\/i> we succeeded)<\/li>\n<\/ol>\n<p><code><br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;title&gt;Page Transitions - RJM Programming - June, 2023 ... thanks to https:\/\/www.w3schools.com\/jsref\/event_onpageshow.asp and https:\/\/www.w3schools.com\/jsref\/event_onpagehide.asp&lt;\/title&gt;<br \/>\n&lt;script type=text\/javascript&gt;<br \/>\n<br \/>\n  function myFunction(opening) {<br \/>\n    if (opening) {<br \/>\n      document.getElementById(\"mya\").href = document.URL.split('?')[0].split('#')[0] + '?rand=' + Math.floor(Math.random() * 19877654);<br \/>\n      document.title='You got to ' + document.URL + ' at ' + ('' + new Date()) + '.';<br \/>\n      document.getElementById(\"myh1\").innerHTML+='&lt;br&gt;' + document.title;<br \/>\n      document.body.style.backgroundColor='lightgreen';<br \/>\n    } else {<br \/>\n      document.title='You left ' + document.URL + ' at ' + ('' + new Date()) + '.';<br \/>\n      document.getElementById(\"solong\").innerHTML+='&lt;br&gt;' + document.title;<br \/>\n      document.body.style.backgroundColor='yellow';<br \/>\n    }<br \/>\n  }<br \/>\n  <br \/>\n  function myCloseFunction() {<br \/>\n    if (document.visibilityState === \"hidden\") {<br \/>\n      document.title='You Left ' + document.URL + ' At ' + ('' + new Date()) + '.';<br \/>\n      document.getElementById(\"solong\").innerHTML+='&lt;br&gt;' + document.title;<br \/>\n      document.body.style.backgroundColor='yellow';<br \/>\n    } else {<br \/>\n      myFunction(true);<br \/>\n    }<br \/>\n  }<br \/>\n<br \/>\n  document.onvisibilitychange = function() {<br \/>\n    if (document.visibilityState === \"hidden\") {<br \/>\n      document.title='You Left ' + document.URL + ' At ' + ('' + new Date()) + '.'<br \/>\n      document.getElementById(\"solong\").innerHTML+='&lt;br&gt;' + document.title;<br \/>\n      document.body.style.backgroundColor='yellow';<br \/>\n    } else {<br \/>\n      myFunction(true);<br \/>\n    }<br \/>\n  };<br \/>\n<br \/>\n  document.onpagehide = function() {<br \/>\n    if (document.visibilityState === \"hidden\") {<br \/>\n      document.title='You Left ' + document.URL + ' at ' + ('' + new Date()) + '.';<br \/>\n      document.getElementById(\"solong\").innerHTML+='&lt;br&gt;' + document.title;<br \/>\n      document.body.style.backgroundColor='yellow';<br \/>\n    } else {<br \/>\n      myFunction(true);<br \/>\n    }<br \/>\n  };<br \/>\n<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body onvisibilitychange=\"myCloseFunction();\" onpagehide=\"myFunction(false);\" onpageshow=\"myFunction(true);\"&gt;<br \/>\n&lt;h1 id=myh1&gt;&lt;\/h1&gt;&lt;br&gt;&lt;br&gt;<br \/>\n&lt;a target=_blank id=mya href=''&gt;Open new webpage incarnation ...&lt;\/a&gt;&lt;br&gt;&lt;br&gt;<br \/>\n&lt;h4 id=solong&gt;&lt;\/h4&gt;&lt;br&gt;&lt;br&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n<\/code><\/p>\n<p>So feel free to try these means by which you can <i>welcome<\/i> and <i>plan for leaving<\/i> respectively in <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/pageTransitions.html\">web application<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d59794' onclick='var dv=document.getElementById(\"d59794\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/event\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d59794' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today, we discuss alternative event ideas to document.body onload and onunload events tracing the navigating to and from a webpage, respectively. The three event types we&#8217;ve experimented with, today, in our proof of concept HTML and Javascript pageTransitions.html, are, with &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/onpageshow-and-onpagehide-and-onvisibilitychange-events-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,37],"tags":[155,4373,4042,4369,400,576,652,870,4371,4370,874,4372,3049,997,1319,1418],"class_list":["post-59794","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-tutorials","tag-body","tag-close","tag-document-visibilitystate","tag-document-window","tag-event","tag-html","tag-javascript","tag-onload","tag-onpagehide","tag-onpageshow","tag-onunload","tag-onvisibllitychanged","tag-open","tag-programming","tag-tutorial","tag-webpage"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/59794"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=59794"}],"version-history":[{"count":7,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/59794\/revisions"}],"predecessor-version":[{"id":60028,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/59794\/revisions\/60028"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=59794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=59794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=59794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}