Opera window.close does not fire/execute after window.print if "Print" clicked in Printer selection dialog - printing

This issue is only with Opera. Other browsers are fine:-
Using JavaScript, I am creating a new window, writing to the document and then calling print and close on the window object.
After the print function executes and the printer selection dialog pops up, if the user clicks "Cancel", the close function executes and the window closes. However, if the user clicks "Print", the document prints however the window does not close.
Here is the JS Fiddle
http://jsfiddle.net/HEaFP/
and here is the code I am using
var ic_hic_demo_window = window.open("", "", "toolbar=no, status=no, directories=no, menubar=no, titlebar=no, location=no, scrollbars=no, resizable=no, height=500, width=500, left=10, top=10");
ic_hic_demo_window.document.open();
ic_hic_demo_window.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title></title>');
ic_hic_demo_window.document.write('</head><body style="font-family: tahoma, arial, verdana; font-size: 11px" onload="print();close();">');
ic_hic_demo_window.document.write("Some sample text");
ic_hic_demo_window.document.write("</body></html>");
ic_hic_demo_window.document.close();
Any help is greatly appreciated...

Well.. I guess it makes sense to ignore a command to close a window if printing from that window is in progress, don't you? Have you tried to do something like
onload="print();setInterval(function(){close();}, 500)"
the idea being that the script keeps trying to close the window every half a second until it works.

Related

navigator.sendBeacon not called from unload

According to MDN and the specs, navigator.sendBeacon is intended to be called from window unload.
Now, it does not seem to work anymore if you close the last tab of your browser, or your entire browser window.
Can anyone confirm if this is by design? If so, is there a workaround to send lastminute data on unload?
I tested with this sample file, in Firefox 74 and Chrome 81, looking for calls with Fiddler.
<html>
<head>
<title>unload test page</title>
<script>
window.addEventListener("unload", function () {
navigator.sendBeacon('https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon');
});
</script>
</head>
<body>
<p><div>unload test page</div></p>
</body>
</html>
MDN states (as of 1/12/2021):
It’s intended to be used in combination with the visibilitychange
event (but not with the unload and beforeunload events)
When visibilitychange transitions to hidden, you can treat that as when the tab/browser is closing and use sendBeacon then.
Example code from MDN:
document.addEventListener('visibilitychange', function logData() {
if (document.visibilityState === 'hidden') {
navigator.sendBeacon('/log', analyticsData);
}
});

Autohotkey+IE WB.document.write() works only TWICE, why?

I'm trying to use IE ActiveX control to update Autohotkey GUI dynamically, but encountering weird behavior. Please help.
; ie-refresh.ahk on Autohotkey 1.1.24
global WB
Gui, Font, s9 cBlack, Tahoma
Gui, Add, Text, , % "Click button to see html content."
Gui, Add, ActiveX, xm w120 h30 vWB, Shell.Explorer
Gui, Add, Button, xm gBtnClicked, % "Update html text"
Gui Show
return
BtnClicked()
{
html_tmpl =
( Ltrim Join
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0px;
color: red;
}
</style>
</head>
<body>
Count: {}
</body>
</html>
)
static snum := 0
snum++
html_code := Format(html_tmpl, snum)
WB.Navigate("about:blank")
WB.document.write(html_code)
}
GuiEscape:
GuiClose:
ExitApp
When I click the button, the IE content updates, but it only updates twice.
On third button click, IE content area almost certainly shows as blank.
Keep clicking the button, the red text appears intermittently and randomly, rougly one out of ten clicks.
So what's wrong with my code?
The problem seems to be with calling navigate the second time.
I thought that WB.Stop() would fix the problem but after that you find out that WB.Navigate is not enough to clean the screen so...
The most sensible alternative looks like putting the WB.Navigate after the gui-add (or somewhere after) and then using WB.Refresh() .
Just for refrerence, some WebBrowser Control documentation here.
Although I cannot explain the weird behavior in my question, I have managed to find out a solution to my requirement.
Use code below:
; ie-refresh.ahk on Autohotkey 1.1.24
global WB
Gui, Font, s9 cBlack, Tahoma
Gui, Add, Text, , % "Click button to see html content."
Gui, Add, ActiveX, xm w120 h30 vWB, Shell.Explorer
Gui, Add, Button, xm gBtnClicked, % "Update html text"
WB.Navigate("about:blank")
Gui Show
return
BtnClicked()
{
html_tmpl =
( Ltrim Join
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0px;
color: red;
}
</style>
</head>
<body>
Count: {}
</body>
</html>
)
static snum := 0
snum++
html_code := Format(html_tmpl, snum)
WB.document.open()
WB.document.write(html_code)
WB.document.close()
}
GuiEscape:
GuiClose:
ExitApp
First, call WB.navigate("about:blank") only once.
Second, when I need to update the whole html document, I need to open + write + close.
Now it works reliably.

Suppress the “are you sure you want to leave this page” popup in the Delphi TWebbrowser control

I have an Delphi application that uses TWebbrowser component to automate navigation to another web application we have.
My problem is that sometimes IE shows the infamous 'Are you sure you want to leave this page' message and when this happens, my app can't navigate to another pages unless an user clicks on 'Leave this page' button. I can't edit the website code to remove this warning, unfortunately.
This message is plaguing my app for weeks, and I could not reach to a proper solution anymore. What I did is to keep a background process do manually send a keystroke when this window is show, but this is not a good solution because nobody can use the computer while my app is working.
I saw possible solution for C# in the topic below but I need a Delphi code instead.
Supress the "are you sure you want to leave this page" popup in the .NET webbrowser control
Any help is very, very appreciated.
Thanks :)
This message is shown by the underlying MSHTML engine if the web page handles window.onbeforeunload event. Usually, it's there for a reason, to let the user know his/her input hasn't been saved or submitted yet. The prompt suppression script from the answer you linked doesn't work for cases when the page uses addEventListener("beforeonload", handler) or attachEvent("onbeforeunload", handler). I don't think there's a reliable way of doing this, without resorting to low-level Windows hooks.
[UPDATE] The following script (look for "Inject this script") is a hack which aggressively suppresses the page's own handlers for onbeforeunload event, via setInterval. It should work in 99% of cases, but it still leaves a gap for the page to override onbeforeonload right before navigating away.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title></title>
<script type="text/javascript">
window.attachEvent("onbeforeunload", function (ev) {
window.event.returnValue = "onbeforeunload via window.attachEvent()";
});
window.addEventListener("beforeunload", function (ev) {
window.event.returnValue = "onbeforeunload via window.addEventListener()";
});
window.onbeforeunload = function (ev) {
window.event.returnValue = "onbeforeunload via window.onbeforeunload";
};
</script>
<script type="text/javascript">
//
// Inject this script
//
(function () {
var onbeforeunloadHandler = function (ev) {
if (ev) {
if (ev.stopPropagation)
ev.stopPropagation();
if (ev.stopImmediatePropagation)
ev.stopImmediatePropagation();
ev.returnValue = undefined;
}
window.event.returnValue = undefined;
}
var handler = null;
var intervalHandler = function () {
if (handler)
window.detachEvent("onbeforeunload", handler);
// window.attachEvent works best
handler = window.attachEvent("onbeforeunload", onbeforeunloadHandler);
// handler = window.addEventListener("beforeunload", onbeforeunloadHandler);
// handler = window.onload = onbeforeunloadHandler;
};
window.setInterval(intervalHandler, 500);
intervalHandler();
})();
</script>
</head>
<body>
Go away
</body>
</html>
To inject this script with Delphi, you'd probably need to resort to low-level WebBrowser/MSHTML COM interfaces, like IWebBrowser2, IHTMLDocument2, IHTMLScriptElement, in a very similar way it's done in the linked answer. With some more efforts, the same can also be done via late binding, using IDispatch::GetIDsOfNames and IDispatch::Invoke only. If you're asking for exact Delphi code, I don't have one.
The other answer you link to handles the browser's Navigated event. In it, it injects a script element into the page and into each frame on the page. That script assigns a new value to window.alert so that when other code on the page calls it, it does nothing.
This code resets the event handler:
var
WrkIHTMLWindow2: IHTMLWindow2;
WrkIHTMLWindow2Disp: IHTMLWindow2Disp;
begin
WrkIHTMLWindow2 := IHTMLDocument2Disp(WrkIWebBrowser2.Document).parentWindow;
if WrkIHTMLWindow2.QueryInterface(IHTMLWindow2Disp, WrkIHTMLWindow2Disp) = S_OK then
if not VarIsNull(WrkIHTMLWindow2Disp.onbeforeunload) then
WrkIHTMLWindow2Disp.onbeforeunload := NULL;
end;

jQuery Tooltip remains open if it's on a link within an iframe (in FF & IE)

I'm replacing the standard "Reset your password" text link with a help' icon, but I discovered that when a jQuery Tooltip is on a link within an iframe, it remains open once the link is clicked until the parent page is refreshed.
I'm using inline frames, but I also experienced the same problem when linking to another page. I tried moving the title inside a <span> tag, as well as closing the iframe and opening a new one with the functions below, but the tooltip just remains open on the page.
UPDATE - I created a fiddle to demonstrate the problem http://jsfiddle.net/chayacooper/7k77j/2/ (Click on 'Reset Link'). I experience the problem in both Firefox & IE (it's fine in Chrome & Safari).
HTML
<img src="help.jpg">
Functions to close iframe and open new iframe
function close_iframe() {
parent.jQuery.fancybox.close();
}
function open_iframe() {
$.fancybox.open([{href:'reset_password.html'}], { type:'iframe'
});
}
I am using jquery-1.8.2, jquery-ui-1.9.1 and fancyapps2
Could be an incompatibility or bug between the fancybox and the jQueryUI tooltip.
Essentially, the fancybox is showing the second form but the browser is not seeing the mouseout event. You can check this by adding a callback function to the .close() event of the jQueryUI tooltip.
$('a[href="#inline_form1"]').tooltip({
close: function( event, ui ) {
console.log('closing')
}
})
You should be able to see closing in the console in IE, Firefox and Chrome when the mouse moves out of the "Reset Link" anchor. However, when clicking "Reset Link" in Chrome you see the closing log line again but in IE9 it does not appear again. So the browser is missing the event.
We can work around this by manually calling .tooltip('close') when "Reset Link" is clicked, like this:
$('a[href="#inline_form1"]').on('click', function() {
$(this).tooltip('close')
})
There is a small problem with the way in which the tooltips are created which means that with just the above click handler it will error with
Uncaught Error: cannot call methods on tooltip prior to initialization; attempted to call method 'close'
This seems to be caused by using the $(document).tooltip() method which uses event delegation for all elements with a title attribute. This is the simplest way of creating tooltips for all elements so I understand why this is used but it can add unnecessary events and handling to the whole page rather than targeting specific elements. So looking at the error it is telling us that we need to explicitly create a tooltip on the element we want to call 'close' on. So need to add the following initialisation
$('a[href="#inline_form1"]').tooltip();
Sp here is the completed JavaScript
$(function () {
$(".fancybox").fancybox({
title: ''
})
$(".fancybox").eq(0).trigger('click')
$(document).tooltip();
$('a[href="#inline_form1"]').tooltip()
$('a[href="#inline_form1"]').on('click', function() {
$(this).tooltip('close')
})
})
Note: You only need one jQuery document.ready wrapping function - the $(function (){ ... }) part :-)

Form value second time not visible when displayed with jQuery ui dialog

I'm displaying some form data in a jquery dialog. Everything works fine when I do this the first time. I can see the "my value" string in the dialog. If I reopen the dialog again for the second time the form value is no longer visible. Check out this jsfiddle to try it out yourself. This is the code:
var dialog;
$("#b1").click(function(){
dialog = $("<div></div>").html("<p><input id='input1' type='text'></p>").dialog({
autoOpen:false,
});
$("#input1").val("my value");
dialog.dialog("open");
});
This bug only happens when I add the html tags dynamically. If I use a static html block everything works fine. Any idea what is wrong here? Thanks!
That is because you are not destroying the old
<input id='input1' type='text'>
so when you call
$("#input1").val("my value");
it sets the value of first
<input id='input1' type='text'>
it finds in the DOM.

Resources