Solved

opening a browser window in JS

Posted on 2012-03-17
6
236 Views
Last Modified: 2012-06-27
I want to display a document in a seperate browser window so I use window.open with the url to the document.

No problems... it works.

However the next time i want to display a document i want to use the same browser window... so the user does not have lots of windows open.

I see there is a windows.location.replace and other methods that maybe i can use... but how can i tell if the window is still open... as i guess then a "replace" would do nothing?

Any help or advice appreciated.
0
Comment
Question by:soozh
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:nap0leon
ID: 37732876
To re-use the same pop-up window, you do just like you would when you open a new browser window or a new tab - add a "target" to the opener function.

e.g.,
function LaunchPopup(page,width,height) {
     OpenWin = this.open(page, "CtrlWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,dependent=no,directories=no,width=" + width + ",height=" + height + ",x=50,y=50");
}

Open in new window


This function happens to have 3 parameters, the URL, height, and width
1- page = the Url you want to display in the page
2- "CtrlWindow" is the name of the window - change this to whatever you want, you could parameterize it so that you can reuse the function to create multiple pop-up windows and selectively target which one to reload every time you call the function
3- All the stuff at the end are the attributes you can assign to the new window.  these can be parameterized, hardcoded, or removed as you see fit.
0
 
LVL 9

Accepted Solution

by:
experts1 earned 500 total points
ID: 37733127
Try example below with parent window and child window
HTML files located in same folder:

Only one child window will open!

Parent window code:
----------------------------------------
<html>
<head>
<title>Close Child Window Show Parent</title>
<script language="javascript">
  var main_win;
function win_child()
{
//var cs=document.getElementById("conno").value;
//var urlstring="?cSearch="+cs;
main_win=window.open("child_window.html", "myWin", "toolbar=no,menubar=no,width=800,height=425,resizable=no");
main_win.focus();
//gogetit(cs);
}

function alert_child() {
alert("child window just closed");
}
</script>
</head>
<body> 
<br>
<input type=button value="Child_Screen" onClick="win_child()">
<br>
<input type=button value="Child_Focus" onClick="javascript:main_win.focus();">
</body>
</html>

Child window code:
----------------------------------------
<html>
<head>
<title>Child_Window</title>
<script language="javascript">
 window.onbeforeunload = function() {
   showParent();
   window.opener.alert_child();
   window.opener.focus();
   alert('onunload event');
}

 function showParent() {
   window.opener.focus();
}
</script>
</head>
<body onunload=" window.opener.focus();"> 
<br>
<input type=button value="Show_Parent" onClick="showParent()">
<br>
</body>
</html>

Open in new window

0
 

Author Comment

by:soozh
ID: 37735109
Hello,

Thanks for the comments.  

I look at nap0lens solution but i could not see how the target was introduced in the open window call.

I am not sure how relevants experts1 solution is.  I know nothing about js.  All i want to do is open a new url in the same window.

Thanks
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 9

Assisted Solution

by:experts1
experts1 earned 500 total points
ID: 37735162
What I submitted was a test example with two web pages
for you to place in a test folder on your machine as follows:

(1) the first file with Parent window code can be named "parent_window.html"

(2) the second file with Child window code is named "child_window.html"

In parent window code you may change the URL name from "child_window.html"
to an external URL like "www.mywebpage.com" or whatever you choose, but the
child window will open in the same window as long as you use the "main_win" object
to open it.

Hope this helps!
0
 
LVL 18

Expert Comment

by:nap0leon
ID: 37735221
The target is CtrlWindow in my example. It is the second parameter in the this.open command.
0
 
LVL 18

Expert Comment

by:nap0leon
ID: 37735251
Perhaps a simpler example... both of these buttons pass 'targetNameHere' as the name of the target window.  

<button onclick="LaunchPopup('popup1.html','targetNameHere')">Popup 1</button>
<button onclick="LaunchPopup('popup2.html','targetNameHere')">Popup 2</button>
<script>
function LaunchPopup(page,target) {
     OpenWin = this.open(page, target);
}
</script>

Open in new window


If you want it to be a popup instead of a new tab, you will need to add back the parameters from the first example I posted that give the window a height and width.
<button onclick="LaunchPopup('popup1.html','targetNameHere')">Popup 1</button>
<button onclick="LaunchPopup('popup2.html','targetNameHere')">Popup 2</button>
<script>
function LaunchPopup(page,target) {
     OpenWin = this.open(page, target, "width=300,height=300");
}
</script>

Open in new window


The other optional parameters you can use (scrollbars, x/y position, etc.) are listed in my first example.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now