Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

opening a browser window in JS

Posted on 2012-03-17
6
Medium Priority
?
250 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 2000 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 9

Assisted Solution

by:experts1
experts1 earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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).

721 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