I am using this code to display some msg before tab is closed in firefox:
function callUnloadFunction(){
return "Hello world";
}
...
window.onbeforeunload = callUnloadFunction;
However, the msg that is displayed is not "Hello world". has firefox stopped listening to that event?
Thanks
Consider using jQuery's unload event.
You should call the event in your function:
window.onbeforeunload = function (e) {
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Any string';
}
// For Safari
return 'Any string';
};
https://developer.mozilla.org/en/DOM/window.onbeforeunload
https://developer.mozilla.org/en/DOM/window.onunload
Related
I am trying to warn the user before closing the tab. I search on many places for the right code but It seems that it doesn't work in chrome.
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
In IE it works fine. I don't need to display any custom message. I just need to warn the user that they will leave the site. Any idea why this doesn't work? Is there any other way to force the browser to display warning before leaving site?
EDITED: I use Google Chrome version 74.0.3729.169
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;
};
Use the confirm() function:
function confirmExit(e) {
if (!confirm("You have attempted to leave this page. Are you sure?")) {
e.preventDefault();
return false;
}
}
I found the solution thanks to #barbsan. The link that he provided contains the solution.
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome etc.
});
https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent
We are using following code to show default pop up on refresh, tab close events
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
var confirmationMessage = 'You are attempting an assessment. Are you sure you want to leave this assessment?';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
}
});
I want to remove and unbind this event at runtime. I tried the following code but no luck.
$(window).off("beforeunload");
$(window).off("onbeforeunload");
window.onbeforeunload = null;
$(window).unbind("beforeunload");
Had to change the event binding code.
function closeIt() {
if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
}
window.onbeforeunload = closeIt;
and unbind used below code
window.onbeforeunload = null;
I have question related to Browser close event..
How to capture the Browser close event without refresh event.
Thanks in Advance.
Using window.onbeforeunload but this will also be triggered when you reload the browser or moving to a different page by clicking on a link.
window.onbeforeunload = function(e) {
var dialogText = 'Dialog text here';
e.returnValue = dialogText;
return dialogText;
};
Note: window.onbeforeunload will not work on touch devices except Android.
Try this one
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;
};
$(function () {
$("a").not('#LogOut').click(function () {
window.onbeforeunload = null;
});
$(".btn").click(function () {
window.onbeforeunload = null;
});
});
i would like to disable 'contextmenu' from keyboard key that is called 'menukey'. the key code is 93.
<script type="text/javascript">
document.onkeydown = function (e) {
if (e.which == 93) {
return false;
}
}
</script>
I've tested the above code on some browser application :
✔ IE 10
✔ firefox 22
✔ Opera 12.16
✘ Chrome 28.0.1500.95
? Safari x.x
✔ succeed | ✘ not worked | ? not tested
is there any guys who can fix?
why it's on chrome does not work?
I don't know why the method you're using doesn't work, but you can do this instead:
window.oncontextmenu = function(event) {
return false;
};
You could
Create a flag if the key was pressed
Add an "keydown" EventListener
store if the user pressed the contextmenu key,
Add an "contextmenu" EventListener
if the key was pressed
Prevent the default action
set the flag to false
var keypressed = false;
window.addEventListener ("keydown",function (e) {
if (e.keyCode === 93) keypressed = true;
});
window.addEventListener ("contextmenu",function (e) {
if (keypressed) {
e.preventDefault(e);
keypressed = false;
}
})
The idea is to hide the contextmenu only on the key pressed, as the title indicates, but still allows it on mouseclicks etc.
If you want to completely disable it, go for #nnnnnn's Answer instead =)
Heres a Fiddle
Only tested in Chrome 29
Disable full keyboard without tab:
document.onkeydown = function (e) {
if (e.which == 9) {
return true;
}
else{
return false;
}
};
I tried to disable onbeforeunload event from frame script with this command:
window.parent.onbeforeunload = null;
but received this dialog:
I tried to debug and onbeforeunload becomes null. But how I can do so this dialog not shown?
For additional information, I need to trigger this event with JS. At start of the page I set:
window.parent.onbeforeunload = confirm;
where confirm is my own function. But in some places of code I need to disable this event and after that enable with the same command.
This could be happening because null is basically an object in Javascript. Here is how I had written it:
var confirmCloseFn = function(evt) {
if (!captureClose) return;
var message = getLogoffMsg();
evt = (evt) ? evt : window.event;
if(message) {
if (evt) evt.returnValue = message;
return message;
}
else {
if (evt) evt.returnValue = null;
return null;
}
};