Why onbeforeunload doesn't fire if page is not clicked? - javascript

I just want to learn why is that?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>close</title>
</head>
<body>
<h1>onbeforeunload</h1>
<script>
console.log("loaded");
window.onbeforeunload = confirmExit;
function confirmExit(){
console.log("closed");
return false;
}
</script>
</body>
</html>
If you copy and save this code as a .html file and run it in browser and don't click inside the page and close the tab or browser, then the onbeforeunload won't fire!
But if you click once inside the page, and then try to close the page, it works all fine as it should.

This is documented behaviour:
Note: To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with; some don't display them at all. For a list of specific browsers, see the Browser_compatibility section.

Try this:-
$(document).ready(function() {
console.log("loaded");
window.onbeforeunload = confirmExit;
function confirmExit(){
console.log("closed");
return true;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>onbeforeunload</h1>

Related

Redirecting to other page after checking on connection status

I have a page that will be redirect to other page after 30 second.. Before it does.. It will first check for the connection status..If the connection were available then it will proceed to redirect to next page.. If connection not available then it will remain at the current page (stay)..
Problem is that after it has detected that the connection is offline then it will stay at the current page even after connection is available without refreshing the page.. How can I recheck for connection status? I mean from offline status and automatically detect the connection availability and move to next page...
<script type='text/javascript'>
function Redirect() {
if (Offline.state === 'up'){
window.location = "../Offline/FPDTest.aspx";}
}
setTimeout('Redirect()', 30000);
</script>
#Muhammad Faiz, use setInterval() method. on body onload event put your function. This method calls regular interval of time. Lets say if you gave 5000 the at every 5 second it will called. Now inbetween when the connection found the next call redirect to your FPDTest.aspx page.
Refer, this link for more study.
For example if you have 2 page Lets say its WebForm1.aspx and WebForm2.aspx then structure of both page will be
WebForm1.aspx
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function Redirect() {
console.log("hello");
if (navigator.onLine) {
window.location = "WebForm2.aspx";
}
}
</script>
</head>
<body onload="setInterval(function(){ Redirect();}, 5000);">
</body>
</html>
Webform2.aspx will be..
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
</body>
</html>
Note: here both page must have its seperate body tag if you follow my example.
You can check it by make your internet connection on/off. meanwhile make sure your localhost is running.
Can listen to online/offline events and do whatever you need for each
window.addEventListener('offline', function(e) {
console.log('offline');
});
window.addEventListener('online', function(e) {
console.log('online');
});
Using that library you could listen to it's up event
Offline.on("up", (ev)=> {
window.location = "../Offline/FPDTest.aspx";
});
This will prevent you from calling the function unnecessarily in an interval. You can unbind the event by doing Offline.off("up");
You can unbind the event after 30 seconds.
setTimeout(()=>{
Offline.off("up");
}, 30000);
please try
<script type='text/javascript'>
function Redirect() {
if (Offline.state === 'up'){
window.location = "../Offline/FPDTest.aspx";}
};
Offline.on('up', function () {
setTimeout(function(){ Redirect();}, 3000);
});
</script>

How to find out who caused the window.onbeforeunload(): F5 or submit?

I work on a small web app and I need to show confirm dialog after user refreshes his browser. I do it using window.onbeforeunload = function() { ...}. The problem is that it is invoked not only on refreshes but after submits as well. I have a question: can I find out somehow if window.onbeforeunload was caused by refresh or by a submit element?
You can write event handlers for various events that would navigate you away from the page and have an identifier to manage state. Then you can use this identifier in onbeforepageunload function to do whatever action you want to perform. Here is a code example that looks for input type submit and sets the identifier. Similarly you can have one for anchor tags and so on.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="">
<input type="submit" value="Submit">
</form>
</body>
<script>
var submit_was_clicked = false;
window.addEventListener("beforeunload", function (event) {
if(submit_was_clicked){
console.log('submit button clicked')
}else{
console.log('on before unload');
}
});
document.addEventListener("click", function(e) {
if (e.target.type.toLowerCase() === 'submit') {
submit_was_clicked = true;
}
}, true);
</script>
</html>

In cordova, how to navigate to another html page?

I am currently working on a Cordova project in Visual Studio. In this project, I am trying building 2 html pages, let me call them first.html and second.html.
In the first.html, I want to add a link to second.html, which allows me to navigate to second.html. I tried 2 ways.
window.location
window.location = "second.html"
tag
<a href=“second.html”></a>
As a result, they both caused an error saying "Exception occurred
Message: Exception: Cannot redefine property: org".
Can anyone tell me how to navigate to a new page properly?
You can navigate to another page using window.location.href. An example is shown below
function(){ window.location.href = "second.html";}
try this it work's for me
<!DOCTYPE HTML>
<html>
<head>
<title>My PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
<script type="text/javascript" charset="utf-8">
function onLoad()
{
document.addEventListener("deviceready", onDeviceReady, true);
}
function onDeviceReady()
{
// navigator.notification.alert("PhoneGap is working");
}
function callAnothePage()
{
window.location = "test.html";
}
</script>
</head>
<body onload="onLoad();">
<h1>Welcome to PhoneGap</h1>
<h2>Edit assets/www/index.html</h2>
<button name="buttonClick" onclick="callAnothePage()">Click Me!</button>
</body>
</html>
You can use the below line to navigate one page to another page.
$('#yourelement').click(function(){
window.location.assign('name_of_page.html');
});
Try this:
window.open("second.html");
window.open opens a new window/tab with the selected URL, while the mentioned method in the question redirects the current page to the selected URL.

Browser close event For Firefox not working

In my application i have to alert user to signout on browser close.
For that i have used the javascript as below
<body scroll="no" onbeforeunload="browerClose()">
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://localhost:8086/egs/ervlet?pri=logOut";
}
this is Working for IE ,but not works for FireFox,
Wats the Problem....Any Suggesstions
Thankxx in Advance,
I would suggest you move the javascript function to the head section of the HTML document. That way it is working for Firefox. Maybe this is because HTML documents are processed in sequential order and you need to define the function before you can use it.
Do something like this:
<head>
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://google.com";
}
</script>
</head>
<body scroll="no" onbeforeunload="browerClose()">
<!-- you code here -->
onbeforeunload event will not work from fire fox version 26 if u used any custom alert message in your application which means you need to perform x operation when closing browser/tab or refreshing page but you put alert message in function which will be called from onbeforeunload then the x (update) operation will not happened.
<html>
<head>
<script>
function unloadfunction() {
alert('test');
// update employee ID in Database
}
</script>
</head>
<body onbeforeunload="unloadfunction();">
</body>
</html>

How to cancel browser close window?

Is it possible to cancel the browser close window event? Or at least add a message prompting the user whether they are sure they want to leave the page? I tried doing this with the beforeunload event, but it still closes the browser. I am looking for something similar to Microsoft's exchange website.
Thanks!
Here is an Html Code:
<html>
<head>
<script language="javascript" type="text/javascript">
window.onbeforeunload = askConfirm;
function askConfirm(){
return "You have unsaved changes.";
}
</script>
</head>
<body>
</body>
</html>
Paste this code into the text area on this page: http://www.htmlpreview.richiebrownlee.com press the button, then exit out of the page that comes up..
a ha!
It appears there's a supported JQuery solution
$(window).unload(function()
{
//...
});
http://www.webhostingtalk.com/showthread.php?t=927093
this should be cross browser supported...hopefully....
window.onbeforeunload
Will work on Safari, IE, Firefox, Chrome, but not Opera.
onbeforeunload should work fine. Bind this on body/window attribute of the html. For more details check this detailed blog post, blog
use onbeforeunload event http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</body>
</html>

Categories

Resources