How to call a function when close popup window - javascript

I am calling the Javascript window.open() function to load another url in a pop up window.
When the users closes the popup window, I want the MyThanks() function to be called. How I can do this?
My Script :
<!DOCTYPE html>
<html>
<head>
<script>
function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
myWindow.focus();
}
function MyThanks(){
alert("Thanks");
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
Regarding usage of popups:
Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to http://www.ekwebhost.com and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?

function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
// Add this event listener; the function will be called when the window closes
myWindow.onbeforeunload = function(){ alert("Thanks");};
myWindow.focus();
}
Try this!
You can swap out the function(){ alert("Thanks");}; for MyThanks to call your function.

The new window that you open will have an attribute called opener that references the Window object that created it. You can then call functions on the parent window by calling window.opener.MyThanks();
Popup:
<script type="text/javascript">
function closePopup() {
window.opener.MyThanks();
window.close();
}
</script>
<div>
<h1>My Popup</h1>
<!-- Form or info to display -->
<button onclick="closePopup();">Close</button>
</div>
While there is nothing inherently wrong with browser popup windows, they can be annoying since the user can lose them behind other windows. Also, it takes their focus off of your page to do something else. The application that I currently develop is trying to move all of our popup windows to Javascript dialogs. It gets pretty annoying for users once you get 3 popups deep.
There are a bunch of ways to create dialogs within your webpage. jQuery UI is one library that I like.

You can try this:
<script>
function openWin() {
myWindow = window.open('http://facebook.com/ekwebhost', '', 'width=600,height=400,left=200');
myWindow.focus();
MyThanks();
}
function MyThanks() {
alert("Thanks");
}
</script>

Related

My angular App freezes when the print window is open

I am making an angular app that requires to create a document, open it in a new tab and print a section. I have already achieved this.
The problem is my client want to continue interacting with the app while the print window is still open.
But I have noticed that when this print window is open, the app like it freezes, all click events no longer work until you close this window.
I have tried a few solutions provided here on Stack Overflow but none really works. In one case I tried setTimeout().
Here is my html code:
<!-- Print button-->
<div class="footer-share" (click)="print()">
<button class="btn btn-back"><span class="mdi mdi-printer"></span><span> Drucken</span></button>
</div>
And here is the funtion in my ts file:
print() {
let printContents, popupWin;
printContents = document.getElementById('print-section').innerHTML;
popupWin = window.open('', 'top=0,left=0,height=100%,width=auto');
popupWin.document.open();
popupWin.document.write(`
<html>
<head>
<title>XYZ Records</title>
<style type="text/css" media="print">
#page { size: landscape; }
</style>
<style>${printStyles}</style>
</head>
<body onload="window.print();window.close()">${printContents}
<h2>This is the end!
</h2>
<img src="/assets/images/tempton-logo.png" style="width:60%;padding-top:0%;" alt="homepage" class="dark-logo" />
</body>
</html>`
);
popupWin.document.close();
}
What can I change to make it possible for the user to continue interacting with the app when the print tab is still open?
Remove the html property and value for onload in .write() method. Then, remove the close method directly after the write method use this instead:
popupWin.focus();
popupWin.print();
popupWin.close();
Try to do what was suggested in this other question, it worked for me.
https://stackoverflow.com/a/62808575/7912859
According to the documentation: (https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features) window.open() currently supports features. Hence we can pass noreferrer or noopener as shown below, which does not freeze or block the parent window.
window.open(hostUrl, '_blank', 'noreferrer')

Close current window when it call Open Tel:

When I call window.location.href("tel:"+'+0060199999') it will open the Skype which is working fine.But i dont want current browser window stay open, i want to close it.If i put close, it is not even open the Skype.Is this possible to do?
<html>
<script>
window.onload = function () {
window.location.href="tel:+0060199999";
window.close();
}
</script>
<body>
Let me test
</body>
</html>

window.open Named Window is opening new window on each click

On launching a window using window.open(), a new instance of browser is getting launched on button click every time, even if the calling function is having named window.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function OpenNamedWindow() {
var w2 = window.open("http://stackoverflow.com", "myWindow", "width=500px;height=500px");
}
</script>
<input type="button" onclick="OpenNamedWindow();" value="Launch App" />
</body>
</html>
After launching the page when I try to access window.name, it shows empty.
I did some search and looks like cross domain is causing the issue.
How can I load the page in same instance instead of launching a new browser window all the time?
This is a trick
function openInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
But even this, perhaps should not work in all browser depending on users configuration
This topic is still here in stackoverflow.
try to check it by yourself at the following link
Open a URL in a new tab (and not a new window) using JavaScript
It opens only one window at a time:
var window_name = null; // global
...
function open_new_window() {
if (window_name && !window_name.closed) {
window_name.close();
window_name = window.open(....);
}

Add a delay to existing code?

I am trying to create an HTML file that when run, will open a URL in a new tab (the URL is pointing to a file on a local server, so when the HTML is run it in fact prompts the user to open or save the file), and then after x seconds, closes itself without the prompt asking the user if they wish to close the tab.
Only Internet Explorer can be used, as my company does not allow other browsers.
I already have a file that opens and closes the tab, however, the tab is closed almost immediately, which does not allow time for the prompt to come on-screen and ask the user if they wish to open or save the file. Here is my code so far:
<html>
<head>
<title>You are being redirected</title>
<meta HTTP-EQUIV="REFRESH" content ="0; url=X:\\Directory\Subdirectory\File.docx">
</head>
<body>
<center>
<script type="text/javascript">
window.open('javascript:window.open("", "_self", "");window.close();', '_self');
</script>
</center>
</body>
</html>
A solution to my problem would be to just literally add a delay between the tab opening and closing in the above code (ideally 5 seconds or 5000 milliseconds). If anyone could show me how to add this delay before it closes that would be extremely helpful.
<script type="text/javascript">
setTimeout(function() {
window.open('javascript:window.open("", "_self", "");window.close();', '_self');
}, 5000);
</script>
You should use settimeout():
setTimeout(function,milliseconds,param1,param2,...)
Read about it here: w3schools settimeout();
This works to open a new tab and close it after 5 seconds
var wnd = window.open("http://google.com", "_blank", "");
window.setTimeout(closeWindow, 5000);
function closeWindow() {
wnd.close();
}

Javascript redirect - New Window

I'm trying to create a re-direct using Javascript from inside a blank iframe that will direct to a URL inside a new window or tab.
More specifically, I'm trying to make a Facebook tab point to my company's webpage rather than load the page inside the tab's iframe which is not the same width as our web page.
Here is the redirect script I already have but it doesn't open the URL in a new window.
<script language="JavaScript">
<!--Redirect to 75th page
var time = null
function move() {
window.location = 'http://philmontscoutranch.org/Camping/75.aspx'
}
timer=setTimeout('move()',0000)
//-->
</script>
Ok - This snippet will open a new window when the page loads but Chrome's pop-up blocker blocked it. I didn't think about this until now. Maybe I could have it load in a light window?
<script language="JavaScript">
<!--Redirect to 75th page
var time = null
function open_win()
{
window.open("http://philmontscoutranch.org/Camping/75.aspx", "_blank");
}
timer=setTimeout('open_win()',0000)
//-->
</script>
Here you go: Jsfiddle Example
function opennewtab(url){
var win = window.open(url, '_blank');
}
<html>
<head>
<script>
function open_win()
{
window.open("http://philmontscoutranch.org/Camping/75.aspx", "_blank");
}
</script>
</head>
<body>
<input type="button" value="Open Window" onclick="open_win()">
</body>
</html>
Source

Categories