{"id":60025,"date":"2023-07-07T03:01:38","date_gmt":"2023-07-06T17:01:38","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=60025"},"modified":"2023-07-06T07:13:41","modified_gmt":"2023-07-05T21:13:41","slug":"applying-onpageshow-and-onpagehide-and-onvisibilitychange-events-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/applying-onpageshow-and-onpagehide-and-onvisibilitychange-events-tutorial\/","title":{"rendered":"Applying Onpageshow and Onpagehide and Onvisibilitychange Events Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/video_browse_preview.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Applying Onpageshow and Onpagehide and Onvisibilitychange Events Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/onpageshow_and_onpagehide_and_onvisibilitychange_events_applied.gif\" title=\"Applying Onpageshow and Onpagehide and Onvisibilitychange Events Tutorial\"  style=\"float:left;\"  \/><\/a><p class=\"wp-caption-text\">Applying Onpageshow and Onpagehide and Onvisibilitychange Events Tutorial<\/p><\/div>\n<p>Yesterday&#8217;s <a title='Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial' href='#oooept'>Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial<\/a> was held off for a while by us, because we couldn&#8217;t think of a good and useful application of these events.  But one occurred to us when we saw &#8230;<\/p>\n<ul>\n<li>on a webpage &#8230; that was &#8230;<\/li>\n<li>playing a video &#8230; when we &#8230;<\/li>\n<li>left it to open another tab&#8217;s webpage for that web browser &#8230; then &#8230;<\/li>\n<li>on revisiting the first webpage its Javascript had paused the video that was playing (and waited for us to restart, as required)<\/li>\n<\/ul>\n<p> &#8230; to inspire us to use newly introduced onpageshow and onpagehide and onvisibilitychange events in <a target=_blank href='https:\/\/www.rjmprogramming.com.au\/ITblog\/video-file-browse-preview-tutorial' title='Video File Browse Preview Tutorial'>Video File Browse Preview Tutorial<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/video_browse_preview.html\">web application<\/a> in <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/video_browse_preview.html-GETME\">its changed<\/a> <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/video_browse_preview.html-GETME\">video_browse_preview.html<\/a> form, except <font color=blue>our version goes<\/font> &#8230;<\/p>\n<ul>\n<li>on <a style=color:blue; target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/video_browse_preview.html\">web application<\/a> webpage &#8230; that <font color=blue>can be<\/font> &#8230;<\/li>\n<li>playing a video &#8230; when we &#8230;<\/li>\n<li>left it to open another tab&#8217;s webpage for that web browser &#8230; then &#8230;<\/li>\n<li>on revisiting the first webpage its Javascript had paused the video that was playing (and <font color=blue>we restart the playing video, rather than have it be that we<\/font> waited for us to restart, as required)<\/li>\n<\/ul>\n<p>The status of all this is reflected by our webpage&#8217;s document.title on its web browser tab.<\/p>\n<p><!--p>You can also see this play out at WordPress 4.1.1's <a target=_blank  href='\/\/www.rjmprogramming.com.au\/ITblog\/applying-onpageshow-and-onpagehide-and-onvisibilitychange-events-tutorial\/'>Applying Onpageshow and Onpagehide and Onvisibilitychange Events Tutorial<\/a>.<\/p-->\n<hr>\n<p id='oooept'>Previous relevant <a target=_blank title='Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/onpageshow-and-onpagehide-and-onvisibilitychange-events-primer-tutorial\/'>Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial<\/a> is shown below.<\/p>\n<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<hr>\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='#d60025' onclick='var dv=document.getElementById(\"d60025\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/video\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d60025' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;s Onpageshow and Onpagehide and Onvisibilitychange Events Primer Tutorial was held off for a while by us, because we couldn&#8217;t think of a good and useful application of these events. But one occurred to us when we saw &#8230; on &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/applying-onpageshow-and-onpagehide-and-onvisibilitychange-events-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":[4,12,14,37],"tags":[155,4373,1816,4042,4369,400,576,652,870,4371,4370,874,4372,3049,3735,2362,997,2001,1319,1369,1404,4382,1418],"class_list":["post-60025","post","type-post","status-publish","format-standard","hentry","category-animation","category-elearning","category-event-driven-programming","category-tutorials","tag-body","tag-close","tag-document-title","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-pause","tag-play","tag-programming","tag-tab","tag-tutorial","tag-video","tag-web-browser","tag-web-browser-tab","tag-webpage"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/60025"}],"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=60025"}],"version-history":[{"count":3,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/60025\/revisions"}],"predecessor-version":[{"id":60029,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/60025\/revisions\/60029"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=60025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=60025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=60025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}