Solved

opening a browser window in JS

Posted on 2012-03-17
6
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

632 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