<html>
<head>
<title>Window Open Focus - RJM Programming - August, 2021 ... thanks to https://stackoverflow.com/questions/6910278/how-to-return-focus-to-the-parent-window-using-javascript</title>
<style>
td { vertical-align: top; }
select { max-width: 320px; }
div { padding-bottom: 5px; }
td:nth-child(1) { background-color: rgb(255,255,0); }
td:nth-child(2) { background-color: rgb(144,238,144); }
td:nth-child(3) { background-color: rgb(173, 216, 230); }
td:nth-child(4) { background-color: rgb(255, 165, 0); }
th:nth-child(1) { background-color: rgba(255,255,0, 0.5); }
th:nth-child(2) { background-color: rgba(144,238,144, 0.5); }
th:nth-child(3) { background-color: rgba(173, 216, 230, 0.5); }
th:nth-child(4) { background-color: rgba(255, 165, 0, 0.5); }
#tht { background-color: rgb(255,192,203); }
</style>
<script type='text/javascript'>

var wois=null;
var awois=null;

var awoisc='';

var url=location.search.split('url=')[1] ? decodeURIComponent(location.search.split('url=')[1].split('&')[0]) : ' ';
var wname=location.search.split('name=')[1] ? decodeURIComponent(location.search.split('name=')[1].split('&')[0]) : ' ';
var height=location.search.split('height=')[1] ? eval(location.search.split('height=')[1].split('&')[0]) : -1;
var width=location.search.split('width=')[1] ? eval(location.search.split('width=')[1].split('&')[0]) : -1;
var left=location.search.split('left=')[1] ? eval(location.search.split('left=')[1].split('&')[0]) : -1;
var xtop=location.search.split('top=')[1] ? eval(location.search.split('top=')[1].split('&')[0]) : -1;

var channelmode=location.search.split('channelmode=')[1] ? decodeURIComponent(location.search.split('channelmode=')[1].split('&')[0]) : ' ';
var directories=location.search.split('directories=')[1] ? decodeURIComponent(location.search.split('directories=')[1].split('&')[0]) : ' ';
var fullscreen=location.search.split('fullscreen=')[1] ? decodeURIComponent(location.search.split('fullscreen=')[1].split('&')[0]) : ' ';

var xlocation=location.search.split('location=')[1] ? decodeURIComponent(location.search.split('location=')[1].split('&')[0]) : ' ';
var menubar=location.search.split('menubar=')[1] ? decodeURIComponent(location.search.split('menubar=')[1].split('&')[0]) : ' ';
var resizable=location.search.split('resizable=')[1] ? decodeURIComponent(location.search.split('resizable=')[1].split('&')[0]) : ' ';
var scrollbars=location.search.split('scrollbars=')[1] ? decodeURIComponent(location.search.split('scrollbars=')[1].split('&')[0]) : ' ';

var status=location.search.split('status=')[1] ? decodeURIComponent(location.search.split('status=')[1].split('&')[0]) : ' ';
var titlebar=location.search.split('titlebar=')[1] ? decodeURIComponent(location.search.split('titlebar=')[1].split('&')[0]) : ' ';
var toolbar=location.search.split('toolbar=')[1] ? decodeURIComponent(location.search.split('toolbar=')[1].split('&')[0]) : ' ';

var replace=location.search.split('replace=')[1] ? decodeURIComponent(location.search.split('replace=')[1].split('&')[0]) : ' ';

function perhapsopen() {
var cdelim='', third='', thisv='';
var arr=['channelmode','directories', 'fullscreen', 'xlocation', 'resizable', 'menubar', 'scrollbars', 'status', 'titlebar', 'toolbar', 'height', 'width', 'left', 'xtop'];
if (url != ' ') {
document.getElementById('url').value=url;
if (wname == ' ') {
awoisc='awois=window.open("' + url + '")';
} else {
if (wname.replace('_blank','').replace('_parent','').replace('_top','').replace('_self','') != '') {
document.getElementById('nondefopt').value=wname;
document.getElementById('name').value=huh;
} else {
document.getElementById('name').value=wname;
}
if ((channelmode + directories + fullscreen + xlocation + resizable + menubar + scrollbars + status + titlebar + toolbar + height + width + left + top).replace(/\-1/g, '').trim() == '') {
if (replace == ' ') {
awoisc='awois=window.open("' + url + '", "' + wname.trim() + '")';
} else {
awoisc='awois=window.open("' + url + '", "' + wname.trim() + '", "' + '' + '", "' + replace + '")';
}
} else {
for (var ii=0; ii<arr.length; ii++) {
eval("thisv='' + " + arr[ii]);
if (thisv.replace(/\-1/g, '').trim() != '') {
third+=cdelim + arr[ii].replace('xlocation','location').replace('xtop','top') + '=' + thisv;
document.getElementById(arr[ii].replace('xlocation','location').replace('xtop','top')).value=thisv;
cdelim=',';
}
}
if (replace == ' ') {
awoisc='awois=window.open("' + url + '", "' + wname.trim() + '", "' + third + '")';
} else {
document.getElementById('replace').value=replace;
awoisc='awois=window.open("' + url + '", "' + wname.trim() + '", "' + third + '", "' + replace + '")';
}
}
}
}
if (('' + window.opener)) { if (('' + document.referer).indexOf(document.URL.split('#')[0].split('?')[0]) != -1) { setTimeout(andthenit, 4500); } else { setTimeout(andthenit, 4500); } } else { setTimeout(andthenit, 4500); }
}

function andthenit() {
if (awoisc != '') { eval('' + awoisc); }
}

function focusMain(doc) {
var win = doc.defaultView || doc.parentWindow;
if (win.opener == null) {
doc.focus();
} else {
focusMain(win.opener.document);
}
}

function setFocusOnMain(el) {
var doc = el.ownerDocument;
var win = doc.defaultView || doc.parentWindow;
focusMain(win.opener.document);
}

function atstart() {
var wonurl='';
if (window.opener) {
wonurl=window.opener.document.URL;
if (wonurl.indexOf('?url=') != -1) { document.getElementById('dstyle').innerHTML=(' <style> * { opacity: 0.5 !important; } </style>').substring(0,1).trim(); }
}
document.getElementById('url').value=document.URL.split('#')[0].split('?')[0];
perhapsopen();
if (!awois && awoisc == '') {
if (document.URL.indexOf('focus=') != -1) {
if (document.URL.indexOf('window_open_focus.htm?end=') == -1) { wois=window.open('./window_open_focus.htm?end=here', '_blank').focus(); }
} else {
if (document.URL.replace('url=','end=').indexOf('window_open_focus.htm?end=') == -1) {
if (document.URL.indexOf('?url=') == -1 && wonurl.indexOf('?url=') == -1) {
window.name="parent";
wois=window.open('./window_open_focus.htm?end=here', '_blank');
//wois.window.opener.focus();
}
}
if (('' + window.opener)) { setTimeout(andthen, 4000); }
}
}
}

function andthen() {
window.opener.document.getElementById('myh4').innerHTML='reached the end';
document.getElementById('myh4').innerHTML='at an end';
if (document.URL.indexOf('?url=') == -1) {
var goback = window.open('', 'parent');
goback.focus();
}
}

function checkit(foro) {
return true;
}

function selit(selo) {
if (selo.value.replace('_blank','').replace('_parent','').replace('_top','').replace('_self','') != '') {
var huh=prompt('Please enter name of window', selo.value);
if (huh == null) { huh=''; }
if (huh == selo.value) { huh=''; }
if (huh.trim() != '') { document.getElementById('nondefopt').value=huh; selo.value=huh; }
}
}

function yesno(osel) {
if (osel.value != '') {
osel.name=osel.id;
} else {
document.getElementById('d' + osel.id).innerHTML=document.getElementById('d' + osel.id).innerHTML.replace(' name="' + osel.id + '"',' ');
}
}

function numit(onum) {
if (('' + onum.value).indexOf('-') == -1) {
onum.name=onum.id;
} else {
document.getElementById('d' + onum.id).innerHTML=document.getElementById('d' + onum.id).innerHTML.replace(' name="' + onum.id + '"',' ');
}
}
</script>
</head>
<body onload=atstart();>
<h1>Window Open Focus</h1>
<h3>RJM Programming - August, 2021 ... thanks to <a target=_blank title='https://stackoverflow.com/questions/6910278/how-to-return-focus-to-the-parent-window-using-javascript' href='https://stackoverflow.com/questions/6910278/how-to-return-focus-to-the-parent-window-using-javascript'>https://stackoverflow.com/questions/6910278/how-to-return-focus-to-the-parent-window-using-javascript</a> and <a target=_blank title="window.open info from W3Schools" href='https://www.w3schools.com/jsref/met_win_open.asp'>W3Schools<a></h3>
<h4 id=myh4></h4>
<form onsubmit='return checkit(this);' method=GET action=./window_open_focus.htm target=_self>
<table border=5>
<tr><th id=tht colspan=4>window.open(URL, name, specs, replace)</th></tr>
<tr><th style=width:50%;>URL</th><th style=width:7%;>name</th><th style=width:38%;>specs</th><th style=width:5%;>replace</th></tr>
<tr><td><input style=width:100%; type=url name=url id=url value='' placeholder=URL></input>
</td><td><select onchange=selit(this); name=name id=name>
<option id=defopt value='' title='Optional. Specifies the target attribute or the name of the window.'>Optionally ...</option>
<option value='_blank' title='URL is loaded into a new window, or tab. This is default'>_blank</option>
<option value='_parent' title='URL is loaded into the parent frame'>_parent</option>
<option value='_self' title='URL replaces the current page'>_self</option>
<option value='_top' title='URL replaces any framesets that may be loaded'>_top</option>
<option id=nondefopt value='name' title='The name of the window (Note: the name does not specify the title of the new window)'>name</option>
</select>
</td><td>
<div id=dleft>left=<input title='pixels The left position of the window. Negative values not allowed' type=number step=1 min=-1 onblur=numit(this); onchange=numit(this); id="left" value="-1"></input></div><!--br-->
<div id=dtop>top=<input title='pixels The top position of the window. Negative values not allowed' type=number step=1 min=-1 onblur=numit(this); onchange=numit(this); id="top" value="-1"></input></div><!--br-->
<div id=dwidth>width=<input title='pixels The width of the window.' type=number step=1 min=-1 onblur=numit(this); onchange=numit(this); id="width" value="-1"></input></div><!--br-->
<div id=dheight>height=<input title='pixels The height of the window. Min. value is 100' type=number step=1 min=-1 onblur=numit(this); onchange=numit(this); id="height" value="-1"></input></div><!--br-->
<div id=dchannelmode>channelmode=<select onchange=yesno(this); id="channelmode"><option value=''>Whether or not to display the window in theater mode. Default is no. IE only</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=ddirectories>directories=<select onchange=yesno(this); id="directories"><option value=''>Obsolete. Whether or not to add directory buttons. Default is yes. IE only</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dfullscreen>fullscreen=<select onchange=yesno(this); id="fullscreen"><option value=''>Whether or not to display the browser in full-screen mode. Default is no. A window in full-screen mode must also be in theater mode. IE only</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dlocation>location=<select onchange=yesno(this); id="location"><option value=''>Whether or not to display the address field. Opera only</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dmenubar>menubar=<select onchange=yesno(this); id="menubar"><option value=''>Whether or not to display the menu bar</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dresizable>resizable=<select onchange=yesno(this); id="resizable"><option value=''>Whether or not the window is resizable. IE only</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dscrollbars>scrollbars=<select onchange=yesno(this); id="scrollbars"><option value=''>Whether or not to display scroll bars. IE, Firefox and Opera only</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dstatus>status=<select onchange=yesno(this); id="status"><option value=''>Whether or not to add a status bar</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dtitlebar>titlebar=<select onchange=yesno(this); id="titlebar"><option value=''>Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
<div id=dtoolbar>toolbar=<select onchange=yesno(this); id="toolbar"><option value=''>Whether or not to display the browser toolbar. IE and Firefox only</option>
<option value=yes>yes</option><option value=no>no</option><option value=1>1</option><option value=0>0</option></select>
</div><!--br-->
</td><td>
<div id=dreplace><select onchange=yesno(this); id="replace"><option value=''>Optional. Specifies history list condition.</option>
<option value=true>true - URL replaces the current document in the history list</option><option value=false>false - URL creates a new entry in the history list</option></select>
</div><!--br-->
</td></tr>
</table>
<br><input style='background-color:yellow;' type=submit value=Open></input>
</form>
<div id=dstyle></div>
</body>
</html>