I want to create a link that says 'Close' and it should close the window. Anyone have any idea how to do this?
I've looked at other people's questions and it seems I can't find anything that works.
This is what I have right now:
<a id="noThanks" href="JavaScript:window.close()">No, thanks</a>
There is javascript command for closing current window.
window.close();
or if you are opening named window
function openWin() {
myWindow = window.open("http://www.domain.com", "_blank", "width=200, height=100");
}
function closeWin() {
myWindow.close();
}
Using win.close() will close the window if win is a variable that has the value of a window.open() call stored in it.
So,
var win = window.open("//stackoverflow.com");
win.close();
will close the window opened by window.open().
However, window.close() only works on a window that was opened by JavaScript. That is, you can't close a window that a user navigated to by clicking a link.
To control a window, you'd have this:
Open Window
Then, in your new window, you'd have this to close it:
<button onclick="win.close();">Close</button>
You can only close a tab/window that you've opened using window.open
Similar question and answers can be found here
This is the answer I was really looking for in case anyone else ever needs it.
Just a heads up--this doesn't work in FF.
window.open('', '_self');
window.close();
In my case once data saved I need close window after confirmation .
<script type="text/javascript">
if (confirm("Close Window?"))
{
close();
}
</script>
Don't forgot to call jquery library before your script
Related
I want to open a window using window.open(). but i have to make sure that if that same window has already opened previously , i have to first close that window then i have to open a new one .
try this code
<button onclick="openNewWindow()">Open New Window</button>
var popup_window;
function openNewWindow(){
if(popup_window != undefined){
popup_window.close();
}
popup_window = window.open("");
}
Try following code
let openedWindow;
let url = "www.example.com"
function openWindow() {
if(openedWindow) {
openedWindow.close();
}
openedWindow = window.open(url);
}
NOTE : The Window.close() method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console:
Scripts may not close windows that were not opened by script.
Check MDN web docs
Basically, I want the current browser tab to be closed on a click of a button. How do I implement this in ReactJS? Tried window.close() but did not work.
For security purposes, JavaScript can't close a window that it didn't directly open.
https://developer.mozilla.org/en-US/docs/Web/API/Window/close
As you can see from the example, the originating script (which opened the window) must also be the script that closes the window. A new window isn't capable of closing itself via JavaScript.
//Global var to store a reference to the opened window
var openedWindow;
function openWindow() {
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow() {
openedWindow.close();
}
On your click event handler, try this :
window.open("", "_self");
window.close();
How to close a window using jQuery for all browser compatibility?
<input type="button"
name="backButton"
value="Close"
style="background-color:#245f91; color:#fff;font-weight:bold;"
onclick="window.close();">
This onclick event is working in IE. Could you help me to write code in jQuery?
$(element).click(function(){
window.close();
});
Note: you can not close any window that you didn't opened with window.open. Directly invoking window.close() will ask user with a dialogue box.
just window.close() is OK, why should write in jQuery?
You can only use the window.close function when you have opened the window using window.open(), so I use the following function:
function close_window(url){
var newWindow = window.open('', '_self', ''); //open the current window
window.close(url);
}
For IE: window.close(); and self.close(); should work fine.
If you want just open the IE browser and type
javascript:self.close() and hit enter, it should ask you for a prompt.
Note: this method doesn't work for Chrome or Firefox.
This will only work for windows which are opened by using window.open(); method. Try this
var tmp=window.open(params);
tmp.close();
i want to open a window in a new tab, but when i want that opener page to be active, not the new one. How can i do this... Many thanks
my code is something like this :
<script language="javascript">
window.open("http://www.google.ro");
window.opener.location.focus();
</script>
To give focus to the new window (but you don't want that, and it will probably have focus by default):
var newWindow = window.open("http://www.google.ro", '_blank');
newWindow.focus();
I don't think it's possible to steal the focus from the new opened tabs. I didn't find any official statement telling this, but all the articles I found on this subject talk about configuring your browser to open tabs by default without focus.
The only "solution" I could come up with is this one:
window.open("http://google.com", "_blank");
window.alert('Hello there! This is a message that annoys you, the user.');
Note that it is possible to shift focus when opening popups.
Learn more about the window object.
This works for me...
var newWindow = window.open('http://www.google.ro', '_blank', 'height=300,width=300,menubar=0,status=0,toolbar=0', false);
window.focus();
To give back focus to the opener window, try using window.focus(window.name); instead of window.opener.location.focus();
Is there any way I can refresh the parent window when a popup window is closed without adding any javascript code to the popup window?
I have a page parent.php on which users can click "open popup" to open a popup window. This popup window shows some flash content and its not possible for me to add something like
window.onunload = function(){
window.opener.location.reload();
};
to the popup window page markup.
Is there any other method to achieve this?
Thanks
To make this work in all major browsers, you need to handle the unload event handler in the pop-up and do the reloading in the main window. In the main window, add
function popUpClosed() {
window.location.reload();
}
In the pop-up:
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
So the answer to your question is generally no, if you need your code to work in all browsers, in particular IE.
I'm sure you can just add this to parent.php:
var myPop = "pop up window selector"
myPop.onunload = function(){
location.reload();
};
The problem with Tim Down's method is that it doesn't answer the original question. The requirement is that you cannot add any code to the pop-up window.
One solution that I've found, while not particularly elegant, is effective across all browsers I've tested on.
You will be simply polling the newly created window object continuously, checking if it's still open.
On parent window:
var register;
var poll;
function isOpen(){
if(register.closed){alert("Closed!"); clearInterval(poll);}
}
function create(){
register = window.open("http://www.google.com","register","width=425,height=550");
poll=setInterval("isOpen()",100); //Poll every 100 ms.
}
had similar problem to detect the closing popup in the parent window. I think the reason was
in not setting the document.domain property.
any way add to Tim Down answer the document.domain property for both window and popup like this:
<script type="text/javascript">
document.domain='<?=$_SERVER['SERVER_NAME']?>';
</script>
and instead of
window.onunload = function() {
if (window.opener && !window.opener.closed) {
window.opener.popUpClosed();
}
};
I used in the popup :
<body onunload="window.opener.popUpClosed();">