How to pop up an alert box when the browser's refresh button is clicked?
You can do it like this:
window.onbeforeunload = function() {
return "Data will be lost if you leave the page, are you sure?";
};
This would show a prompt to the user allowing them to cancel. It's not refresh specific, but for your purposes (like editing a question on SO) that doesn't seem to matter, it's loss of info no matter where you're leaving to.
Alert show up when i refresh page
You Should have to try this...
<?php
if(isset($error_message)){
$message = "Grazie per averci contattato. Ci metteremo in contatto con voi molto presto.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
?>
Alert not displaying after page refresh/reload (Angular v10)
Reloading the page re-initialises the application which means that your showAlertMessage = false;
is being executed again once the component loads.
A simple way to get the behaviour you want is to use url params. On button click you modify the path you're on to include something like ?showAlertMessage=true
so that on 2nd load the initial state differs. (example)
Alternative options to it are as Andre mentioned, using the localstorage or some other method of persisting state outside of the app scope.
You might also want to consider if reloading is the right solution here since re-initialising the whole application takes time and goes a bit against the whole idea of a SPA.
Showing alert after reloading page
There are a lot of ways to do it. An easy way to achieve this would be passing through a parameter in the URL and checking it on page load.
// Check the URL parameter on page load
$(function(){
if(getUrlParameter('success') == '1') {
bootstrap_alert.warning('Message has been sent.');
}
});
// Set up the click event
$('#sentcontact').on('click', function(){
if (true){
var nextUrl = window.location.href;
nextUrl += (nextUrl.indexOf('?') === -1 ? '?' : '&') + 'success=1'
window.location = nextUrl;
}
});
// Simple function to read parameters out of the URL
function getUrlParameter(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
JavaScript alert box on page refresh
Use localStorage
with beforeunload
and unload
:
window.onbeforeunload = function() {
var text1 = document.getElementById("text1").value;
var text2 = document.getElementById("text2").value;
localStorage.setItem("match", JSON.stringify(text1 == text2));
};
window.onload = function() {
if (JSON.parse(localStorage.getItem("match"))) {
alert("Texts match!");
} else {
alert("Texts don't match!");
}
};
Alert not showing on browser close after doing reload of the browser, same issue is there in stackoverflow website also
Try this, quite similar to this: Clear localStorage on tab/browser close but not on refresh:
window.onbeforeunload = function (ev) {
window.onunload = function () {
let msg;
debugger;
if (sessionStorage.getItem('previousPage') === 'employee') {
return 'Are you sure you want to navigate away?If you navigate away, your information will not be saved.'
ev.preventDefault();
}
return msg;
}
};
How can i refresh after 10 seconds when alert box shows up?
You can't, if you use confirm
(or alert
or prompt
, any of those relics of the 1990s). They bring the main JavaScript thread to a screeching halt (mostly, there's nuance around this now), meaning you can't do anything else (mostly).
To do this, you need to replace the confirm
with a DOM element showing the message instead, which won't block the main JavaScript thread.
Related Topics
Php 7 With Phpmyadmin Gives Lots of Deprecation Notices
How to Create Multi Level Category Hierarchy ( Category Tree ) - Codeigniter
Merging Two Json by Adding Their Value
Change Language of Site With a HTML Button
Laravel 5 - How to Access Image Uploaded in Storage Within View
Comparing Two Dates in a SQL Request
PHP Parse/Syntax Errors; and How to Solve Them
How to Get the Client Ip Address in PHP
Check If Username and Email Already Exists in Database
How to Stop PHP Code Execution
How to Get Next Id of Autogenerated Field in Laravel for Specific Table
Multiple Column With Same Search String in Laravel
Fatal Error: Call to Undefined Function Mysqli_Connect()
What Is the Regular Expression to Validate a Comma Delimited List But Ending With '&' and a Word