HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML onpagehide Attribute
Photo Credit to CodeToFun
🙋 Introduction
The onpagehide
attribute is a useful feature in HTML that allows developers to execute JavaScript code when a page is about to be hidden or unloaded.
This attribute is often employed to perform cleanup tasks or save session-related data before the user navigates away from the page.
🎯 Purpose of onpagehide
The primary purpose of the onpagehide attribute is to provide developers with a way to execute scripts just before a user leaves a page.
This can be valuable for performing actions such as saving user preferences, recording analytics data, or releasing resources to optimize performance.
💎 Values
The onpagehide
attribute accepts JavaScript code as its value. This code will be executed when the page is being hidden or unloaded.
It is commonly used in conjunction with the window object to define event handlers.
📄 Example
Here's a simple example of how to use the onpagehide
attribute in an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page with onpagehide Attribute</title>
</head>
<body onpagehide="handlePageHide()">
<h1>Your Page Content Goes Here</h1>
<script>
function handlePageHide() {
// Perform cleanup or save data before the page is hidden
console.log("Page is about to be hidden. Perform cleanup here.");
}
</script>
</body>
</html>
🧠 How it Works
In this example, the onpagehide
attribute is added to the <body> element, and it calls the handlePageHide() function when the page is about to be hidden.
🔄 Dynamic Values with JavaScript
You can dynamically set the onpagehide
attribute using JavaScript.
This can be beneficial when you want to customize the behavior based on certain conditions or user interactions. Here's a brief example:
<script>
// Dynamically set onpagehide event handler
window.onpagehide = function() {
// Custom logic to be executed when the page is about to be hidden
console.log("Dynamic onpagehide event handler executed.");
};
</script>
🧠 How it Works
In this script, the onpagehide
attribute is dynamically set to a function that will be executed when the page is about to be hidden.
🏆 Best Practices
- Use the
onpagehide
attribute for tasks that should be performed just before the user leaves the page, such as saving user preferences or analytics data. - Be cautious with resource-intensive operations in the onpagehide event, as it may affect the page unloading performance.
- Test the functionality across different browsers to ensure consistent behavior.
🎉 Conclusion
The onpagehide
attribute is a valuable tool for executing JavaScript code just before a page is hidden or unloaded.
By leveraging this attribute, developers can enhance the user experience and perform necessary cleanup tasks.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML onpagehide Attribute), please comment here. I will help you immediately.