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;
}
};
Related
I'm working on a chrome extension that simulates a keypress to submit a form. This works great as long as I stay on the page and do not switch tabs. When I switch tab the form doesn't get fill out the same.
What's strange is that it's the replaceCommentHtmlAndSetSender function that doesn't seem to propagate information properly on after one comment has been submitted and keypress event is fired. If I click back in to the page then the extension works.
I've tried viewing all events in the console using monitorEvents(window) and they seem to be the same. I've also tried adding a window.focus() event in replaceCommentHtmlAndSetSender without luck.
What events / differences could leaving the tab lead to this behavior?
var replaceCommentHtmlAndSetSender = function(reply_link, resp, id) {
$(document.activeElement).sendkeys("txt")
setTimeout(function() {
var str = '<span data-text="true" data-comment-sender="true">txt >/span>'
$(document.activeElement).replaceWith(str)
})
submitComment()
}
var submitComment = function() {
var foundElementScript = document.createElement("script")
foundElementScript.textContent = "foundElement()"
document.head.appendChild(foundElementScript)
foundElementScript.parentNode.removeChild(foundElementScript)
}
// this script is injected into the page
function foundElement(){
found=document.querySelectorAll('[data-comment-sender="true"]')[0];
found.setAttribute("data-comment-sender", "false");
submitComment(found);
}
}
function submitComment(element) {
var event;
if(document.createEvent) {
event = document.createEvent("HTMLEvents");
event.initEvent('keydown', true, true);
} else {
event = document.createEventObject();
event.eventType = 'keydown';
}
event.eventName = 'keydown';
event.keyCode = 13;
event.which = 13;
if(document.createEvent) {
element.dispatchEvent(event);
} else {
element.fireEvent("on" + event.eventType, event);
}
}
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;
});
});
Hello I want to call API when user close browser so I have use bellow function
window.onbeforeunload = function (event) {
var message = 'Sure you want to leave?';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
}
so this function showing me two buttons "Leave" and "stay"
I want to call API if user click on leave button , but I am not getting any event because it directly close when I click on leave so anyone have any suggestion how can I do this.
As per the project requirement, on which I am working now, I need to show user a javascript alert only in the event of browser close using javascript. All other page events like url click, button click, F5 key press etc. are to be disregarded. I have tried with the following code but with no use.
var isPostBack = false;
$(function() {
// You would copy this for select and any other form elements I forgot about
$('input').live('click', function() { isPostBack = true; });
$('a').live('click', function() { isPostBack = true; });
document.onkeydown = function(e) { //attach to key down event to detect the F5 key
isPostBack = false;
if (!e) { //Firefox and Safari gets argument directly.
e = window.event;
}
var key = e.keyCode ? e.keyCode : e.which;
try {
if (key == 116) { //F5 Key detected
isPostBack = true;
}
}
catch (ex) { }
}
});
window.onbeforeunload = check;
function check() {
if (!isPostBack) {
// Do your unload code
isPostBack = false;
var strPath = window.location.pathname;
if (strPath.indexOf('CustomerPortal') >= 0) {
alert('Customer, you are leaving our page.');
}
else {
alert('User, you are leaving our page.');
}
return "Are you sure you want to exit this page?";
}
}
Please help to achieve my target requirement with your valuable comments and help.