Javascript open new windwo
I have a few buttons that when clicked open a new page. I found a script that will open the window and if the window gets hidden and I click the same button again it will focus back on it.
The problem is that if I click a button and a window opens and I click on another button it opens in the first buttons window. This is the script that I have. How can I get it so that they open in individual windows?
Thanks!
Script:
// pop-up function
var newWin = null;
function closeWin(){
if (newWin != null){
if(!newWin.closed)
newWin.close();
}
}
function popUp(strURL,strType,strHeight,strWidth) {
closeWin();
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
newWin = window.open(strURL, 'newWin', strOptions);
newWin.focus();
}
if (!(navigator.userAgent.indexOf("ozilla/3.")>=1))
{
document.write("<script language=\"JavaScript1.3\" src=\"\/_scripts\/formHighlight.js\">");
document.write("</script>");
}
ROB posted this at 17:25 — 25th June 2003.
They have: 447 posts
Joined: Oct 1999
your windows need unique names
<script language="javascript" type="text/javascript">
var popupOptionStr = 'height=400,width=600,top=50,left=50,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes';
var windowHandles = new Array();
function popup(that) {
windowHandles[that.name] = window.open(that.href, that.name, popupOptionStr);
windowHandles[that.name].focus();
return false;
}
</script>
<a name="mypage" href="mypage.html" onclick="return popup(this);">my page</a>
<a name="mypage2" href="mypage2.html" onclick="return popup(this);">another page</a>
zollet posted this at 17:27 — 25th June 2003.
He has: 1,016 posts
Joined: May 2002
Below is a code that I've wrote that I use on all my websites. It centers the window on the screen sets focus on it.
function newWindow(nURL, nTarget, nWidth, nHeight, nOptions) {
var nLeft = (screen.width - nWidth) / 2;
var nTop = (screen.height - nHeight) / 2;
var nWindow = window.open(nURL, nTarget, 'width='+nWidth+',height='+nHeight+',left='+nLeft+',top='+nTop+','+nOptions);
nWindow.window.focus();
}
Here's an example how you can use it...
<a href="javascript:void(0);" onClick="javascript:newWindow('http://www.google.com/', 'Window_Google', '740', '400', false);">Open new window</a>
So basically, it is newWindow('http://www.domain.com/dir/', 'windowname', 'width', 'height', 'options');
If you use different windownames on different links they will open in separate windows.
I hope this helps.
nitestarz posted this at 17:36 — 25th June 2003.
They have: 38 posts
Joined: Dec 2002
Thank You!
nitestarz posted this at 17:51 — 25th June 2003.
They have: 38 posts
Joined: Dec 2002
All of the buttons are going to be no particular size but I do have one that needs to be a certain size. How can I do that?
I tried:
but that doesnt seem to work.
ROB posted this at 20:24 — 25th June 2003.
They have: 447 posts
Joined: Oct 1999
this should make that work
var popupOptionStr = 'height=400,width=600,top=50,left=50,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=y
es';
var windowHandles = new Array();
function popup(that, optstring) {
if(!optstring) optstring = popupOptionStr;
windowHandles[that.name] = window.open(that.href, that.name, popupOptionStr);
windowHandles[that.name].focus();
return false;
}
</script>
popupOptionStr is the default options, if you don't send an option string it will use the default
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.