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();
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
So I'm trying to test whenever popup windows are enabled for my site. My window should be blocked by browser's popup blocker, unless they are already allowed. If popup windows are allowed everything works fine, since I can test whenever the window was opened directly in my parent page.
Problem is that I still want to mark the test as sucessfull if the user allows to open popup window later by allowing the popup window to open. Only way for me to do that is by calling some function on my parent page, but window opened this way has null window.opener and I can't set the object on child window directly from my parent window since I don't know when will it open.
Is there any way to get reference to my parent window from child window when it's opened this way?
Or any other workaround that could call function on my parent window whenever the child window is sucessfully opened?
Maybe event event that I could hook up to whenever such window is opened, but I couldn't find any such event.
Sample code:
function () {
var myWin = window.open('popupWin.html', '_blank', 'resizable=no,scrollbars=no,height=1,width=1', true);
if (!myWin || myWin == null || typeof (myWin) == 'undefined')
SetPopupResult(false);
else {
SetPopupResult(true);
myWin.close();
}
}
popupWin.html is empty page with following script:
<script>
window.opener.SetPopupResult(true); //throws Cannot read property 'SetPopupResult' of null
setTimeout(function () {
window.close();
}, 1000);
</script>
Edit: Forgot to mention this is issue in Google-Chrome so far, firefox doesn't seem to have this problem and window.opener is correct, IE's behaivor is different as it refreshes the page and openes the popup window like they were enabled.
I have a page in which i m calling another popup by window.open method. The only thing how can i change a label in opener page from popup page while the popup page is still alive ie which is not closed yet
It's better to let the opener window take care of changing values by exposing a small API to the popup window.
I've outlined it here: javascript - pass selected value from popup window to parent window input box
It should be like this:
window.opener.document.getElementById('label1').value = "the new value";
<script>
function myFunction() {
var additionalWindow = window.open("/additional");
// Write on the additional window
additionalWindow.document.write('written from separate window');
// Call a function on the additional window
additionalWindow.someFunction();
}
</script>
Here's Mozilla's documentation on window.open().
Is it possible to check if same window is already opened?
For example i have opened a window via javascript.
Can i check if that's opened on another page via javascript?
Just want to focus on page if it's been opened already to avoid duplicate windows.
Thanks ;)
Look at window.open() method. You must specify the name of the window as the second parameter. If there already is a window with that name, then the new URL will be opened in the already existing window, see http://www.w3schools.com/jsref/met_win_open.asp
If you really want to check, if the window is opened by your own scripts, then you have to keep a reference to the opened window in a global variable or the likes and create it with
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
You can also encapsulate this behavior in a method:
var myOpenWindow = function(URL) {
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
myOpenedWindow.location.href= URL;
myOpenedWindow.focus();
}
And call that function with myOpenWindow('http://www.example.com/');
If you have parent--child window then here is a solution that will allow you to check to see if a child window is open from the parent that launched it. This will bring a
focus to the child window without reloading its data:
<script type="text/javascript">
var popWin;
function popPage(url)
{
if (popWin &! popWin.closed && popWin.focus){
popWin.focus();
} else {
popWin = window.open(url,'','width=800,height=600');
}
}
</script>
<a href="http://www.xzy.com"
onclick="popPage(this.href);return false;">link</a>
one more thing ::--- If the user refreshes the parent window, it may loses all its
references to any child windows it may have had open.
Hope this helps and let me know the output.
This will help if you want to open a url from a link
var Win=null;
function newTab(url){
//var Win; // this will hold our opened window
// first check to see if the window already exists
if (Win != null) {
// the window has already been created, but did the user close it?
// if so, then reopen it. Otherwise make it the active window.
if (!Win.closed) {
Win.close();
// return winObj;
}
// otherwise fall through to the code below to re-open the window
}
// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
Win = window.open(url);
return Win;
}
newTab('index.html');
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();">