changing window.beforeunload message - javascript

I'm wondering if can I change the window.beforeunload message. All examples on the internet are similar to this:
$(window).bind('beforeunload', function() {
return 'You have unsaved changes. If you leave the page these changes will be lost.';
});
That's cool, and in Google Chrome my message will be displayed, but in Firefox the default message is displayed. How can i trick Firefox to display my message and not the default message?

onbeforeunload is a weird event. Browsers have been debating what to do with it for a while.
IE and Chrome will display your message in the dialog along with their own message.
Firefox used to display your message, but in version 4+, they stopped supporting custom messages. See this: https://bugzilla.mozilla.org/show_bug.cgi?id=588292
Opera doesn't even support the onbeforeunload method!
This method is under debate because it can be used for evil, and also because it can confuse/annoy users. Scammy, virus-laden sites can use messages like:
"Leaving the page will mean your computer may still be infected, please stay and install our virus scanner"
Nowadays, websites can use AJAX / localStorage to save changes, so this event isn't really needed.

Related

Redirect to a new page using Browser or Tab Close Event in (Chrome, Firefox and IE) All of them

Please do not mark this as duplicate, as this is not being answered completely in any single post. also no recent working solution for latest browsers is found.
My Question is as under, i am using Asp.net C#, and the code should work on client-side preferably.
Is it Possible to Catch Browser/Tab Close Event for Non IE Browsers (ex. Chrome, Firefox) Current Versions [Version 81+ for Chrome (64-bit)], and Execute navigate to logout page.
The code which i tried for Chrome primarily is as below.
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = 'this is cuctom message';
window.location.href = "~/logout/Logout.aspx";
});
Now when i use above code in IE, it gives me message like
the problem which i am facing with above code is.
On Every link click this message is called in IE, along with tab close too, and i want to call this message only on tab or browser close.
for Chrome though, above code is called only on link navigation, but not redirected to logout page, and not on browser close.
I am stuck in this activity, with no way forward, can anyone please help me out with a way towards solution to this issue.

How to read Chrome/Firefox Pop up actions Leave/Stay when we the leave the page?

When ever u made some change to page and try to leave chrome/firefox throws a alert. Data you entered may not be saved with (Leave or Stay) buttons.
How i can do certain action when user clicks leaves or stay.
To which event i need to listen to to make actions when we click leave or stay?
TIA
There is no window close event.You can use onbeforeunload to prevent asking for simple confirmation etc.
That is what is done internally by website developers on their page. But you can't read that answer from an user script. It is simply the website developers implementation.You can't read user's response given to website developers confirm question by user from an application.
Those popups are implemented by the corresponding websites not browser.
Solution
Modern browsers now consider displaying a custom message to be a security hazard and it is removed therefore. Browsers now only display generic messages. So we can simply do this:-
window.onbeforeunload = function() {
return false;
};
window.onbeforeunload = null; // this will not show any popup.
So just remove everything fron onbeforeunload and return false and make it null. This will solve your problem.
Note: You can't show custom message or do anything significant using onbeforeunload. That is the answer to your question originally asked.

Fullscreen not working on Firefox 30

When I am invoking fullscreen mode on Firefox 30 I am getting this error message on Firefox's console -
Request for full-screen was denied because Element.mozRequestFullScreen() was not called from inside a short running user-generated event handler. jquery.fullscreen.js:182
Everything working fine on Chrome.
I am using this approach for fullscreen : https://github.com/private-face/jquery.fullscreen
You have probably asked the browser to go on fullscreen directly from the code. It can only happen in an eventHandler called from an user action (like a click or keypress).This is what the user message is telling. As you can see, the eventHandler must run fast too. It must be under 1s.
It is for security reasons, so you cannot go fullscreen without requiring the user for confirmation.
Please see the note at MDN or the W3C specification.
There is an javascript confirm dialogue box in between the click of fullscreen button and $.fullscreen() in js side,
Solved the problem by skipping js alert on Firefox browsers.. Still thinking why it works fine on Chrome.

Best way to warn user that he is quitting the page

I've seen a lot of question on this and the solution seems to be
window.onbeforeunload
But i've tried it, seems to work great to warn the user before the pages unload, but there is no way i've seems to be able to set the message in the message box.
i've tried this :
window.onbeforeunload = function (){ return "test";}
But I've got the default message of the browser.
I'm using the french version of firefox 8.0
Thanks all
Firefox does not allow you to change the message, but webkit (Chrome/Safari) does. In webkit, if you return a string, in your onbeforeunload handler, it will interpret that as "pop a warning dialog" with your string as the message.
Sadly onbeforeunload is quite raw and not that great. Hopefully browsers figure out a better way to do this in the near future.

Prevent browser from closing in asp.net

I am using asp.net 2.0 with c#.
i want a pop up to be displayed when user tries to close the browser and if user click on "no" [i.e. he don't want browser to be closed] then it prevent browser to get closed.
Please help.
Thanks
the code they use is
window.onbeforeunload=function() {
if (somereasonorother) return "You did not save your stuff"
}
Pointy, this is entirely possible, and it's done by many web pages for perfectly reasonable reasons.
Try something like this:
function areYouSure() {
return "Are you sure you want to leave this page?";
}
window.onbeforeunload = areYouSure;
You can try to attach yourself to the onbeforeunload event:
<body onbeforeunload="ConfirmClose();">
But I have to mention that it won't work on all browsers. The only ones that prompted something after I closed a window were Chrome, Firefox and Internet Explorer; Opera ignored the code in the JavaScript method.
This is mostly because some browsers trigger the onbeforeunload event only when you're trying to leave the current page by visiting another one, and not when you close the current window / tab.

Categories

Resources