I have creating a new browser window win by #new-window-idclick. And I have events system with that window. Closing win.closed for example. Everything is work until I go to links inside the created window.
$('#new-window-id').on('click', function(){
var finalUrl = $(this).attr('href');
var win = window.open(
finalUrl,
'fullWindowMode');
console.log('created '+ win.name);
var timer = setInterval(function() {
if(win.closed) {
console.log('closed '+ finalUrl);
clearInterval(timer);
document.getElementById('#iframeID').contentWindow.location.reload();
}
}, 1000);
});
So I have iframe on main page. I want to open it in a new window and edit a content there. Then after closing created window, my main page iframe should be updated.
Steps:
Click the link #new-window-id/ //window is opened.
Close window. win.closed is work!
When doesn't work
Steps:
Click the link #new-window-id/ //window is opened.
In the window edit blog post. (url was changed)
Close window. event win.closed doesn't work!
But it breaks if I surfing inside created window. Exists way to keep this connection?
I did the following:
1. Created a window let w = window.open('http://ya.ru')
2. Navigated in that window (within the same site)
3. Checked w.closed in the main window (false)
4. Then closed the popup and checked again (true).
Looks like w.closed works even after navigation.
But if you navigate to another origin in the new window, then location.reload() will become inaccessible from the main window.
Related
I have a HTML page which I have opened in a new window. Now I want only that new window to be active until it is closed using close button (cursor should work on the new window only).
I have tried using window.focus() but I don't think it is the right way to do so.
var mywindow = window.open("editparameter.html", "Modify","height=600,width=800,left= 300,top = 100, location = no, modal=yes,alwaysRaised=yes");
mywindow.focus();
This is the code I have written in my current page. Here editparameter.html is the page that opens in the new window.
if you want to block your page or do something else while child tab is opened, you can open new tab and wait its close:
function open() {
// do something with current tab
const tab = window.open('https://stackoverflow.com/');
tab.onclose = onTabClosed;
}
function onTabClosed() {
// revert what has been done
}
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();
I need to reload current location(href) via JS :
I have a button that open a new window and when that window closed I need to reload the location in window that was initiator(parent). It is work in my browser, but one said me that his window does not reload( he has last version chrome and mac book)
I have the followin code:
var loginWindow = window.open(...);
loginWindow.focus();
loginWindow.onbeforeunload = function() {
location.reload(); //when the popUp window close
//I even tried this:
window.location.href = window.location.href;
}
but the page not reloaded
AS onbeforeunload does not work here it is recommended to check if the window exists with certain interval Please find the reference link below
Check this post
I have a page A which opens a javascript window as myWin = window.open(..). Now in another page B in the same domain, when user clicks on a link, I want to check if myWin is available, if yes then bring that window in front, else do a new myWin.
The problem I have is the window.js file in both pages and window.js contains the line
var myWin = null;
Within Page A scope, my logic works and brings window to front. Within Page B it works as well. Howver when I open window in Page A, click on link in Page B, it fails.
It is as if myWin is reset to null in scope of Page B. How can I overcome this problem? Pointers??/
The popup window is created by page A and only page A contains a reference to that window. When you open page B, there is no way to pass page A's reference to the popup over to page B. This cannot be done.
You want to call window.open with an empty url, e.g.
var newWindow = window.open('', title, dimension); // title needs to be a constant
if (newWindow.location.href && newWindow.location.href!='about:blank') {
// Window was already open and points to something
newWindow.focus();
} else {
// new window make it point to something
newWindow.location.href = 'http://yoursite';
}
This assumes that the popup is opening a window within the same site as the calling page, since otherwise you can encounter various permissions issues.
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');