make only a particular window active until it is closed in js - javascript

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
}

Related

Move to tab but don't reload

I'm using the code below with window.open to open a new link (same origin) in a new tab. If a tab with the new link is already open, the open tab should be focused instead.
This is all working fine, the focused tab is however always refreshed when it gains focus by this. How can I stop the focused tab from refreshing?
window.open(`./${_id}`, `module_${_id}`);
Check if the window is closed orelse use focus(), this should not refresh the page.
var windowObject = null; // global variable
function openURL(url) {
if(windowObject == null || windowObject.closed) {
windowObject= window.open(url);
} else {
windowObject.focus();
}
}
<button value="Check" onClick="openURL('https://www.google.com')"></button>

Lost reference created window after surfing

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.

How to return focus on parent window from a pop window while loading first time

I am working on a website. Where while loading website first time opening index page with one pop window. First my problem is I want to minimize this pop window automatically while loading first time and focus return to website without any click in website.
Here is my code in index page I write this function:
myFunction();
function myFunction() {
//var myWindow = window.open("music.php", "", "width=320,height=60");
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
//myWindow.moveTo(0, 0);
}
And pop window having page extension music.php which should be minimize automatically and focus return to index page without any click on website.
I have already try with set the focus of a popup window to website everytime , but it was also not working.
Well, The window object has an opener property as well.
When we open the browser, that property is null, and when we open a popup , it points to the window the popup has been opened through.
I think something like this should suffice.
myFunction();
function myFunction() {
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
myWindow.opener.focus() // can also do window.focus()
}
The execution of JS should not stop, but if it does, you have to add the focus code to your child page/popup page.(Depending on varying browsers)
like
popupHTMLJS.js
focusParent();
function focusParent(){
parent = window.opener;
parent.focus()
}

Javascript/JQuery: focus the popup window if already opened but not to reload it

Have the following code to open and focus the window popup, unless it hasn't opened yet. It works fine,
But the problem is after focus to the previous opened popup window, can't prevent reloading it and it makes loss of the data on it.
So how can prevent the existing window not to reload to retain the existing data when the link is clicked again?
window.open(url,"searchPatron","height=600,width=1000, status=yes,toolbar=no,menubar=no,location=no").focus();
Maybe it fits your needs:
DEMO
window['windows'] = {};
var url = "//testit.com";
$('button').click(function () {
var popup = window['windows'][url]? window['windows'][url]:
window['windows'][url] = window.open(url, "searchPatron", "height=600,width=1000, status=yes,toolbar=no,menubar=no,location=no");
popup.focus();
});

Javascript: Check for duplicate opened window

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');

Categories