The following code shows an Example of onLoad() and onBeforeUnload() in JavaScript.
Basically, the onLoad event fires when user loads a page or refreshes the page by pressing the F5 button. The following example shows that we have a function f1() that gets executed when user opens the page. Similarly, when user clicks on the link present in the page, a warning message appears.
<html>
<head>
<title>JavaScript Events</title>
</head>
<body id="b1" onload="f1()" onbeforeunload="return f2()">
<a href="http://google.com">Google</a>
<script>
function f1()
{
alert('hi');
}
function f2()
{
return 0;
}
</script>
</body>
</html>
Output
When user tries to navigate away from the abobe page, the onBeforeUnload event fires.
Further Reading
Evolution of JavaScript from ES1 to ES2020
Introduction to HTML DOM Methods in JavaScript
Understanding Document Object Model (DOM) in JavaScript
What is Asynchronous JavaScript?
Understanding HTTP Requests and Responses
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML