onunload and onbeforeunload not working - javascript

I have this script in my aspx page
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = function(e) {
return 'Bye now!';
};
</script>
But this is not invoked when I close the browser. I need to pop an alert asking the user to confirm. I need to do this in the aspx page using javascript. The browser I tried was mozilla firefox. Thanks

The following code should fix your issue:
(function () {
window.unloader = function(e) {
(e || window.event).returnValue = null;
return null;
};
window.addEventListener("beforeunload", window.unloader);
})();
You are not going to be able to specify a message, it is defaulted by the browser.

Related

Trying to disable the browser's back button

I've written two HTML files:
Login.html
Next Page
Home.html`
<html>
<body>
<a href = >Login.html>>Prev Page</a>
</body>
<script type = "text/javascript" >
history.pushState("anything", "", "#1");
window.onhashchange = function (event) {
window.location.hash = "a";
};
</script>
</html>
`
I'm trying to disable browser's back button. If i execute this code on chrome it doesn't disable the back button but if i run history.state command in console of Home.html page and then i click the back button, then it remains on same page(works as expected). Why so?
FOA, Thanks everyone for your answers.
Finally below code gave me the solution:
history.pushState(null, null, window.location.href);
history.back();
window.onpopstate = () => history.forward();
You can't disable the browser's back button. If you could, that would be a security hazard and the browser vendors would most likely seek to fix it.
It is generally a bad idea overriding the default behavior of web browser. Client side script does not have the sufficient privilege to do this for security reason.
There are few similar questions asked as well,
How can I prevent the backspace key from navigating back?
How to prevent browser's default history back action for backspace button with JavaScript?
You can-not actually disable browser back button. However you can do magic using your logic to prevent user from navigating back which will create an impression like it is disabled. Here is how, check out the following snippet.
(function (global) {
if(typeof (global) === "undefined") {
throw new Error("window is undefined");
}
var _hash = "!";
var noBackPlease = function () {
global.location.href += "#";
// making sure we have the fruit available for juice (^__^)
global.setTimeout(function () {
global.location.href += "!";
}, 50);
};
global.onhashchange = function () {
if (global.location.hash !== _hash) {
global.location.hash = _hash;
}
};
global.onload = function () {
noBackPlease();
// disables backspace on page except on input fields and textarea..
document.body.onkeydown = function (e) {
var elm = e.target.nodeName.toLowerCase();
if (e.which === 8 && (elm !== 'input' && elm !== 'textarea')) {
e.preventDefault();
}
// stopping event bubbling up the DOM tree..
e.stopPropagation();
};
}
})(window);
This is in pure JavaScript so it would work in most of the browsers. It would also disable backspace key but key will work normally inside input fields and textarea.
Recommended Setup:
Place this snippet in a separate script and include it on a page where you want this behavior. In current setup it will execute onload event of DOM which is the ideal entry point for this code.
Working Demo link-> http://output.jsbin.com/yaqaho
The HTML5 History API gives developers the ability to modify a website's URL without a full page refresh. This is particularly useful for loading portions of a page with JavaScript, such that the content is significantly different and warrants a new URL.
You can check this link it may helpful
https://css-tricks.com/using-the-html5-history-api/

window.onbeforeunload = confirmExit; not working on android chrome browser

I have the following piece of code which works fine on PC and laptop safari , mozilla and chrome browsers but not on mobile
why and how to replace the code with other working code so that it works in the same way
<script language="JavaScript" type="text/javascript" async>
window.onbeforeunload = confirmExit;
function confirmExit()
{
document.getElementById('ControlCode').style.display = "none";
document.getElementById('ControlCode').style.visibility = "hidden";
}
</script>
I've worked on it.
I can confirm to you that to obtain equal behavior you have to bind the callback like so:
window.onbeforeunload = window.onunload = function(){
/** YOUR CODE HERE **/
};
Best if placed in top of the page, to get it executed also on page load not completed.

detect exit event in IE and Fireforx

I want to detect the exit event on all browser, I tried this code :
<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function (event) {
var message = 'Important: Please click on \'Save\' button to leave this page.';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
};
</script>
</head>
<body>
<p>
Exit your browser ....
</p>
</body>
</html>
this code works in Chrome and safari but it it does'nt work in IE 11 and Fireforx 50.0.1 ... Any ideas ?
From the MDN page following notes are being mentioned
Note also, that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.
The hidden keys inside about:config can be found with the keys dom.disable_beforeunload and dom.require_user_interaction_for_beforeunload.
Your code seems fine for the rest, so it may help to look in your config files (The note mentions mobile browsers, however I have these settings on my Desktop browser as well)
Try with
window.addEventListener('beforeunload', function(evt){ ...
this is the response that I find, It work for me.
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome
});
thank you

Does Google Chrome support body onunload() function?

I m write code as follows:
<script type="text/javascript">
function onBack(){
window.history.forward(-1);}
<body onunload="onBack();" onpageshow="if (event.persisted) onBack();">
Its working on firefox and IE but not on google crome?
Please, Help...... :)
try this:
window.onbeforeunload = function() {
return "Are you sure?";
};
Use Jquery unload. that supported by all browser
$(window).unload(function () {
alert("byebye");
});
in safari and chrome(version 14 and above) onbeforeunload and onunload event is being ignored.

how to stop page redirect

I need to stop all the page redirection through javascript.
I have some script which will redirect the page to some other location.
How can I stop all the page redirection on my page through jquery or javascript.
You can stop the redirect by doing the following.
<script language="JavaScript" type="text/javascript">
//<![CDATA[
window.onbeforeunload = function(){
return 'Are you sure you want to leave?';
};
//]]>
</script>
Not sure if all browsers support this but that should do the trick.
If I'm understanding you correctly, you need to stop users navigating away from the page?
If so, Marcos Placona's answer is one part of it - although all what you actually need is:
$("a").click(function(e) { e.preventDefault(); });
You also don't want people to hit F5 I'm guessing? Well that's harder. Preventing defaults on key press, cross-browser is horrible. But a couple of codes that work in some browser are below here:
function disableF5() { // IE
document.onkeydown = function() {
if (window.event && window.event.keyCode == 116) { // Capture and remap F5
window.event.keyCode = 505;
}
if (window.event && window.event.keyCode == 505) { // New action for F5
return false; // Must return false or the browser will refresh anyway
}
}
}
function disableF5v2() { // FIREFOX
$(this).keypress(function(e) {
if (e.keyCode == 116) {
e.preventDefault();
return false;
}
});
}
However, the cross-browser issue can partly be solved with -
window.onbeforeunload = function(e) {
var message = "Your confirmation message goes here.", e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = message;
}
// For Safari
return message;
};
Hope that all helps anyway. The last bit of code is from another question on this site here.
Rob
Are you trying to stop an iframe breaker? Something like:
<script language="JavaScript" type="text/javascript">
<!--
function breakout_of_frame()
{
if (top.location != location) {
top.location.href = document.location.href ;
}
}
-->
</script>
If so, you can not.
If it's only redirects through link clicks, in jQuery you can do this:
$(".someClass a").unbind('click');
if it's an existing page redirect, you'll need to give us some more information about what kind of redirect this is, so we can help further.
Hope this helps you
AFAIK this won't be possible, since page redirection can occur in many ways. He can click on an anchor tag, can submit a form with different action, set window.location to another page on button click and invoke server code to redirection.
And you won't be able to detect many of these.

Categories

Resources