Click to See Complete Forum and Search --> : Opening up new windows in the browser


troynb
02-26-2003, 06:48 PM
I have a question about opening new pages when a link is clicked on a webpage. What I want to happen is this:


On the main webpage, I would like to have links to other websites. For example:

Link 1
Link 2
Link 3

Now, if I was to click links 1,2 and 3, I would have 4 browser windows open: The window containing the original webpage, plus the 3 windows that were opened by clicking on the 3 links. (I am using a href="www.,,,,," target="_blank"></A>)

Now, what I want to happen is this, I only want the mainpage to have ONE child window so that at any given time, there will be at most 2 browser windows open, the main page window and one child that is opened by clicking on a link. If I click on Link 1 followed immediately by Link 2, I don't want 2 child windows to pop up. What I need to happen is when I click Link 1, it needs to open a window. When I click link 2, the browser window that contains the webpage from link 1 now needs to show the webpage that from link 2. Is there a way to do this using javascript window.open?

Something like this would be best:
<A HREF="javascript:void(window.open('newlink.html'))">LINK</A>

I know this can be done simply buy using a targe like the following:
<a href="1.html" target="_mywindow">my link 1</a>,
<a href="1.html" target="_mywindow">my link 2</a>,
<a href="1.html" target="_mywindow">my link 3</a>,

Unfortunately I need a way to do this using javascript or code that can be coded directly in the HREF=" " section of code. Any suggestions?

AdamBrill
02-26-2003, 06:57 PM
Try this:
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language=javascript>
win=null;
function Open_Window(url)
{
if(win){
win.close();
}
win = window.open(url);
}
</script>
</head>

<body>
<a href="page1.htm" onclick="Open_Window('page1.htm'); return false;">page1</a>
<a href="page2.htm" onclick="Open_Window('page2.htm'); return false;">page2</a>
<a href="page3.htm" onclick="Open_Window('page3.htm'); return false;">page3</a>
</body>

</html>
Hope that helps. :)

Charles
02-26-2003, 06:58 PM
First, never use href="javascript:". For at least one in ten users JavaScript will fail. Second, window.open takes a second parameter that is the name of the window and if a window with that name already exists that window is reused. Hence:

<a href="http://www.w3.org/TR/REC-html32" onclick="window.open(this.href, 'child'); return false">HTML 3.2</a>

<a href="http://www.w3.org/TR/html4/" onclick="window.open(this.href, 'child'); return false">HTML 4.01</a>

BilEde
02-26-2003, 07:09 PM
You can do this by holding the handle of the new window in a variable and using this to manipulate it
addresses= new Array(4)
addresses[0]="Page1.html";
addresses[1]="Page1.html";
addresses[2]="Page1.html";
addresses[3]="Page1.html";

var status=0;
var newWin;

function changeWindowPage(Page){
if(status==0){
newWin = window.open(addresses[Page],"new window", menubar=yes, width=100,height=100);
status=1;
else
newWin.document.location=addresses[Page];
}
return false;
}


I don't know why its a problem to use the onclick event but your link would look like <a href="JavaScript:changeWindowPage(0);">Link</a>. To use the onclick, return a value of false to stop navigation. <a href="#" onclick="return changeWindowPage(0);">Link</a>
(change 0 to the value you want)