I want to close the current tab in Chrome when the print button in Print Dialog is clicked by the user. I tried with window.print() and setTimeout(), but this will close the dialog even if the print is canceled by the CANCEL button.
Is there is any solution for this?
This is what I have done so far:
function printThis(){
window.print();
setTimeout(function(){
window.close();
},5000);
}
You can call the this method in onload, once this print dialog is dismissed the alert prompt will automatically be called and ask the user whether print or not if print has been done, the user will be automatically redirected to the page.
If you want to close the window you should open the particular window by window.open method, you cannot close the window by just opening by URL or redirects.
Example:
window.open("https://yourside.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
Then you can call window.close in the window which is opened by window.open.
window.onload = function () {
window.print();
setTimeout(function(){ if(confirm("Have you printed ?")) { window.location.href="/your_page"; } }, 1);
}
please use this code.
window.addEventListener("beforeprint", function(event) { ... });
//or
window.onbeforeprint = function(event) { ... };
window.onbeforeprint will be called when press Ctrl+p or else for opening print dialog
e.g
window.addEventListener("beforeprint",
function(event) {
window.close();
});
or if you want to close window after print button pressed, please use window.onafterprint like this
window.onafterprint = function(){
window.close()
}
You can use onafterprint event:
window.onafterprint = function(){ window.close() }
Very simple!
on print page (In my case it is "print-page.html") use this script -
<script>
window.onafterprint = function(){
window.close()
}
</script>
It will close the new tab when the user clicked the print/cancel button! Tested on chrome and works fine.
Make sure you have already called the print interface using this method -
<input type="button" value="button name" onclick="window.open('print-page.html','popUpWindow','height=687,width=1057,left=10,top=10,scrollbars=yes,menubar=no'); return false;" />
Best of luck!
Related
I had opened a popup window from a modal now I need to refresh the page where my modal is by closing popup window or by a close button i had tried writing some codes i found here but not working some of my codes are
//this is not working
function closeAndRefresh(){
opener.location.reload();
window.close();
}
Call below function on onClose popup/onclick close button
function closeAndRefresh() {
location.reload();
}
function closeAndRefresh() {
window.top.location.reload();
}
I have the below HTML, which redirects the page and closes the current window
<a href="http://www.google.com" onclick="close_window()">
I tested it, it can redirect page OR close window only, but cannot do both at the same time, that means after redirecting to another page, it does not close the window. Could you advise how to make it close the window after redirect?
You will need Javascript to do this. Use window.close():
close();
Note: the current window is implied. This is equivalent:
window.close();
or you can specify a different window.
So:
function close_window() {
if (confirm("Close Window?")) {
close();
}
}
with HTML:
close
or:
close
You return false here to prevent the default behavior for the event. Otherwise the browser will attempt to go to that URL (which it obviously isn't).
Now the options on the window.confirm() dialog box will be OK and Cancel (not Yes and No). If you really want Yes and No you'll need to create some kind of modal Javascript dialog box.
you have to try window.open for this:
<html>
<head></head>
<body>
<script type="text/javascript">
function test(){
testWindow = window.open("http://www.google.com");
setTimeout(function() { testWindow.close() },5000);
}
test();
</script>
</body>
</html>
I want to close a session in my application when user closes a window by pressing the close button in the title bar or using the Alt + 4 keyboard combo.
I am able to handle X using the code below, but this is not handling Alt + F4. I also tried onbeforeunload but this is not calling the function at all. I am testing with IE9.
<body onunload = "closingWindow()">
<script>
function closingWindow(){
if((window.event.clientX<0) || window.event.clientY<0)){
//Ajax call to close session
}
}
</script>
Check out window.onbeforeunload
https://developer.mozilla.org/en/DOM/window.onbeforeunload
Live Demo
window.onbeforeunload= function(){
// Ajax call to close session
}
Hit alt+f4 in the demo and you will notice the alert before it closes as well.
I am having 2 pop up screens in my (Main) jsp.
In the first pop-up the user will update the required information(Update) and after submitting the info, a new pop up will be shown that shows the modifications(View).
I would like to refresh the Main page when the user clicks on the close "X" in the view page.
I tried to use some scripts like the following in the view page, but it did not work:
<script language="JavaScript">
function refreshParent() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow)
{
window.opener.progressWindow.close()
}
window.close();
}
</script>
Try putting this javascript code in your popup window:
window.onunload = function(){
window.opener.location.reload();
};
function ref_and_close()
{
opener.location.reload();
self.close();
}
//just call the function
ref_and_close();
I have a pop-up window a user logs into, once they are logged in successful, I have a message that has a link to close the window. But I want it to not only close that pop up window, but I want it to refresh the webpage the pop-up window was clicked on.
So the page can refresh to see that there is a valid login session for that user.
Is this possible w/ jQuery?
In your popup window:
$('#closeButton').click(function(e) {
window.opener.location.reload(true);
window.close();
e.preventDefault();
});
Reloads the parent page and closes the popup.
You can do this:
window.location.reload()
It just tells javascript to reload the page, this is not dependent on jQuery.
onclick="javascript:window.opener.location.reload(true);self.close();"
Here is a code that refresh parent window and closes the popup in one operation.
<script language="JavaScript">
<!--
function refreshParent() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow) {
window.opener.progressWindow.close()
}
window.close();
}
//-->
</script>
Use this code in ajaxlogin.js file for mvc 4 user
$('#closeButton').click(function(e) {
window.opener.location.reload(true);
window.close();
e.preventDefault();
});
Its working fine
Works efficiently for me:
$(window).unload(function() {
if (!window.opener.closed) {
window.opener.__doPostBack('', '');
e.preventDefault();
}
});
Use this code in window close event like
$('#close').click(function(e) {
window.opener.location.reload();
window.close();
self.close();
});
http://www.delhincrjob.com/blog/how-to-close-popup-window-and-refresh-parent-window-in-jquery/