Close window after print - javascript

I have a page that when it loads, I want it to open the print dialog, and then when the print dialog is closed, the window needs to close. I have tried many things. I currently have:
<script type="text/javascript">
window.onload = PrintMe;
window.onafterprint = function () { window.close() };
function PrintMe() {
window.print();
}
</script>
In Edge, this seems to be working fine. I chrome, it's working some of the time, which leads me to believe that it's a timing issue. Sometimes the page will load and the show the print box, and close when the print box closes. Sometimes, the page closes before it loads. Sometimes the page not close after the print box is closed. Any ideas?

Related

windows onbeforeunload doesn't work Javascript

I want to prevent a close tab or close browser in a web page.
I am using this code:
<script type="text/javascript">
window.onbeforeunload = function(e) { //Doesn't work well
return 'Exit';
};
$('#form').submit(function() { //No alert on submit form (this works)
window.onbeforeunload = null;
});
</script>
If I click somewhere in the page the code works fine and ask me if I really want to close the page, but if I reload the page and then I close the tab without click something in the page the tab close without show any alert.
Do you have some idea how to fix?

Opening an alert outside of the popup window in a Chrome Extension

I have a Chrome extension I am writing where I have a button on the popup page, that when pressed should open an alert with a little help text for the user. I can get the alert to appear in the popup window by calling:
var button = document.getElementById("HelpMe");
button.addEvenListener("click", function(){
alert("HelpText");
});
But this opens the alert in the popup window, which puts it mostly offscreen. I want to make the alert appear in the main tab, which would put the alert right in the center of the screen.
I tried this:
var button = document.getElementById("HelpMe");
button.addEventListener("click", function(){
chrome.tabs.executeScript(null, {
code:`
alert("HelpText");
`
});
});
But it didn't seem to do anything, and I can't get any error messages or logs to appear that I have been able to find. Advice is appreciated.

Safari print issue with javascript window.print()

I am having an issue with print on Safari. My System is Windows 7, and this function works fine in all other browsers except Safari. Here is the situation:
window.onload = function(){
console.log('before print');
window.print();
}
It won't output the log in console panel, but the print page will appear first, after i choose cancel in print page, the log will be output.
Does any body came up with this issue? Any help will be appreciated.
Updated
Here is the situation i have:
We need to print a page whose content can be changed by user by checking and unchecking check box, and only the content part of this page should be printed, so we create a new page that only contains the content for printing. In this page, we need to hide the unnecessary content that is not selected by user, so we need to do some DOM operation before window.print() get called. The console.log() is just an example code for observing. I tried to add an <div id='test'>Test HTML</div> in test HTML and add
var test = document.getElementById('test');
test.style.background = 'yellow';
before window.print();, it shows the same result in my Safari browser, the 'Test HTML' will not turn to yellow until i click cancel button in print panel, so it's not just the console.log issue.
Updated
I am using Safari 5.1.7(7534.57.2) on Windows 7
For me, the setTimeout solution didn't work. I found this jQuery plugin https://github.com/jasonday/printThis that has plenty of workarounds for window.print() because it seems not to be fully supported by all browsers.
I took this line that worked for me Safari document.execCommand("print", false, null)
and this worked ok for me for now in safari and chrome
try {
document.execCommand('print', false, null);
}
catch(e) {
window.print();
}
This is odd behavior. I tested in Safari 6.1 on Mac.
But may I ask why you need to log something before the printing? Because it seems that all the functions are being executed before the printing panel pops up:
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
window.onload = function() {
$('body').html('before print');
console.log('before print');
window.print();
};
</script>
When you look at the print preview, the page will have the text "before print" on it. For some reason, the console will log the text only when the print panel closes, but in my opinion that doesn't really matter for your visitors. You can manipulate DOM and change the page before the printing process as you like.
After several times trying, below code works, but i don't know the reason, can anybody explain? Or this is a Safari Bug?
window.onload = function() {
$('body').html('After change');
setTimeout(window.print, 1000);
};
Safari prints the page before it is loaded unlike other browsers. Hence window.onload() can be used in the code of the newly opened html page. But if the page opened is non html content, then it is not possible. The below solution is global across browsers and type of content open.
var printWindow = window.open(url, '_blank');
$(printWindow).load(function()
{
this.print();
});
Adding one more solution which worked for my case:
First make your popup window.
$( ".myButton" ).click(function() {
var url = 'www.google.com';
var printWindow = window.open( url, '_blank');
printWindow.focus();
});
Then, inside the HTML page which is loaded in the popup:
$(window).bind("load", function() {
setTimeout( function () {
try {
document.execCommand('print', false, null);
}
catch(e) {
window.print();
}
}, 500);
});

unable to close tab after log-out in firefox

i tired to find out way to close tab after log out in Firefox.
<script type='text/javascript'>
window.open('','_self');
window.close();
document.execCommand('ClearAuthenticationCache');
</script>
working in other browser but problem in Firefox. i need to close tab when signout/logout attempt.
Firefox will not allow you to close windows or tabs that a user opened.
Most financial sites for this reason will ask users to click a button and open a new window.
you can do this with window.open. Store the handle returned by that and use it to close the window you opened. The user may have many other tabs open in that window.
Refer: window.close
This is how you close a window that you open.
var openedWindow;
function openWindow()
{
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow()
{
openedWindow.close();
}

In Firefox, how do I bring an existing popup window with multiple tabs to the front using javascript from the parent window?

I would like to have a button on a web page with the following behavior:
On the first click, open a pop-up.
On later clicks, if the pop-up is still open, just bring it to the front. If not, re-open.
The below code generally works in Firefox, Safari, and IE8 (see here for Chrome woes). However, I have found a failure mode in Firefox that I don't know how to deal with:
If for some reason the user has opened a second tab in the pop-up window and that second tab has focus within that window, the popupWindow.focus() command fails to have any effect. (If the first tab has focus within that window, everything works just great.)
So, how can I focus the popup and the desired tab in Firefox?
<head>
<script type="text/javascript">
var popupWindow = null;
var doPopup = function () {
if (popupWindow && !popupWindow.closed) {
popupWindow.focus();
} else {
popupWindow = window.open("http://google.com", "_blank",
"width=200,height=200");
}
};
</script>
</head>
<body>
<button onclick="doPopup(); return false">
create a pop-up
</button>
</body>

Categories