473,721 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if a window is open

I have a main webpage that has a list of records, each with a link to a
window.open function call. As an example, a page that opens is
editrecord.aspx ?RecordID=34, and another is
editrecord.aspx ?RecordID=52. If the user starts changing the contents
of the opened window, and then leaves it open and goes back to the main
page, refreshes the list, and clicks on the same record link again, it
reloads the contents of the page the user had changed, and all the
changes they made are lost. Is there a way to check to see if the
window for that record is open before we reload it, so that I can just
call the focus() function on that window instead of window.open again?

Mar 16 '06 #1
6 34279

Jack wrote:
I have a main webpage that has a list of records, each with a link to a
window.open function call. As an example, a page that opens is
editrecord.aspx ?RecordID=34, and another is
editrecord.aspx ?RecordID=52. If the user starts changing the contents
of the opened window, and then leaves it open and goes back to the main
page, refreshes the list, and clicks on the same record link again, it
reloads the contents of the page the user had changed, and all the
changes they made are lost. Is there a way to check to see if the
window for that record is open before we reload it, so that I can just
call the focus() function on that window instead of window.open again?


Greetings,

Here is a solution that will allow you to check to see if a child
window is open from the parent that launched it. This will bring a
focus to the child window without reloading its data:

<script language="JavaS cript">
function loadUniquePage( page) {
if (opener && !opener.closed) {
opener.focus();
}
else {
var myWin = window.open(pag e,'','width=800 ,height=600');
opener = myWin;
}
}
</script>

<a href="javascrip t:loadUniquePag e('http://www.google.com' )">link</a>

Notice that the link has to be formated like it is above when you
include it in your page for this to work. Just pass it the URL you want
to have opened and voila!

** NOTE: If the user refreshes the parent window, it loses all its
references to any child windows it may have had open.

I have placed this code on my server if you want to just copy the code
from there:

http://www.manifestinteractive.com/u...ildWindow.html

Hope this helps,
- Peter Schmalfeldt

Mar 16 '06 #2
Manifest Interactive said on 17/03/2006 8:26 AM AEST:
Jack wrote:
I have a main webpage that has a list of records, each with a link to a
window.open function call. As an example, a page that opens is
editrecord.as px?RecordID=34, and another is
editrecord.as px?RecordID=52. If the user starts changing the contents
of the opened window, and then leaves it open and goes back to the main
page, refreshes the list, and clicks on the same record link again, it
reloads the contents of the page the user had changed, and all the
changes they made are lost. Is there a way to check to see if the
window for that record is open before we reload it, so that I can just
call the focus() function on that window instead of window.open again?

Greetings,

Here is a solution that will allow you to check to see if a child
window is open from the parent that launched it. This will bring a
focus to the child window without reloading its data:

<script language="JavaS cript">


The language attribute is deprecated, type is required:

<script type="text/javascript">

function loadUniquePage( page) {
if (opener && !opener.closed) {
Global variables should be declared with the 'var' keyword, just like
other variables, but outside the scope of other objects.

It's also a bad idea to use 'opener', which is already a DOM 0 property
of window objects - it refers to the window that opened this current
window, if there is one.

var popWin;
function loadUniquePage( page)
{
if (popWin&& !popWin.closed && popWin.focus){

Methods of host objects should be tested before use.

opener.focus();
This will not guarantee that 'opener' is brought to the front, but worth
a try. If the window is at the back, nothing will seem to be happening
for some users - no new page, no 'focused popup, nothing. They will
have to close the pop-up to get it to work again.

That may not be desirable, but it's a 'feature' of using pop-ups this way.

}
else {
var myWin = window.open(pag e,'','width=800 ,height=600');
opener = myWin;
Why create a local variable only to re-assign the reference to the
already created global variable?

popWin = window.open(pag e,'','width=800 ,height=600');

}
}
</script>
The full script:

<script type="text/javascript">
var popWin;
function popPage(url)
{
if (popWin &! popWin.closed && popWin.focus){
popWin.focus();
} else {
popWin = window.open(url ,'','width=800, height=600');
}
}
</script>


<a href="javascrip t:loadUniquePag e('http://www.google.com' )">link</a>


And users without script support will see a link that does nothing.
Better to use:

<a href="http://www.google.com"
onclick="popPag e(this.href);re turn false;">link</a>
At least then non-scripted UAs can navigate to the link.

[...]
--
Rob
Mar 17 '06 #3
RobG said on 17/03/2006 10:05 AM AEST:
[...]
The full script:

<script type="text/javascript">
var popWin;
function popPage(url)
{
if (popWin &! popWin.closed && popWin.focus){
Aggh, gremlins in the works...

if (popWin && !popWin.closed && popWin.focus){

popWin.focus();
} else {
popWin = window.open(url ,'','width=800, height=600');
}
}
</script>


[...]

--
Rob
Mar 17 '06 #4
RobG said the following on 3/16/2006 7:05 PM:
Manifest Interactive said on 17/03/2006 8:26 AM AEST:


<snip>

<a href="javascrip t:loadUniquePag e('http://www.google.com' )">link</a>


And users without script support will see a link that does nothing.
Better to use:

<a href="http://www.google.com"
onclick="popPag e(this.href);re turn false;">link</a>
At least then non-scripted UAs can navigate to the link.


And if there is an error in popPage then the JS enabled user gets nothing :)

<a href="..." onclick="return popPage(this.hr ef)">

And have popPage return false at the end.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 17 '06 #5
What if main page goes through a postback and the variable is lost? is
there a way to still check if a child window created using the
window.open method still exsits.

Is there a way if I know the name of the window or is there a way to
store the child window handle so that after a refresh on the main page
I can still check if that same page is still open?

Then if that is true can i still setfocus on that child window?

Mar 17 '06 #6
Greg said the following on 3/17/2006 9:26 AM:
What if main page goes through a postback and the variable is lost? is
there a way to still check if a child window created using the
window.open method still exsits.
No. Once the reference is lost it is lost. You have to recreate it and
you run into the focus problem where applying focus() to the window may
focus it but doesn't bring it back to the front.
Is there a way if I know the name of the window or is there a way to
store the child window handle so that after a refresh on the main page
I can still check if that same page is still open?
No.
Then if that is true can i still setfocus on that child window?


See above.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2180
by: W. | last post by:
Hi all ... I have a page with 4 frames in it. If I click on a button in the 4th frame, a new window has to be opened and the value of the listbox in the 3rd frame hoas to be passed .... My code works fine in IE, but doesn't in Netscape ..... First, the value of the listbox .... I try to get it with parent.frame3.form.listbox.value ... But an alert of this (as a test) does nothing .... I tried several combinations like parent.frames...
2
3508
by: Samir Pandey | last post by:
Hello, I am using the following javascript code to open a new window. Somehow, IE always opens a new window. It doesn't open target url in the window name given. All i want is, if there is a window "MyWin" already open, then the main window should load the target url in "MyWin" and not open a new window again.
5
15044
by: Mark | last post by:
Hi all, I have a website where I want to be able to 'pop up' a window using the window.open call in JavaScript. I remember back in the old days of classic ASP there was a problem where a call to window.open started a new session and all session variables were lost. I did a quick check in ASP.Net and the session variables were saved between the parent window and the new 'pop up' window so that's fine. What I want to know is there any...
4
3598
by: Csaba Gabor | last post by:
Up until a few weeks ago, javascript code like window.open("http://mydomain.com", "windowName"); would always bring my new or reused window to the top, with focus. Lately, Firefox (Deer park alpha 2) only brings it to the top if the window is new. If it's being reused, the window does not come to the foreground (with IE 6 it does). Is there a new way in Mozilla/Firefox that I can ensure that this
13
22170
by: tochiromifune | last post by:
Hello The window.open method brings my window to the top only if it is new. If it's being reused, the window does not come to the foreground (with IE 6 it does). Is there a new way in Mozilla/Firefox that I can ensure that this window comes to the top? Thank you for your help
3
6149
by: harikrishnan.kamalakannan | last post by:
Hi, When a open an asp page from a modal window through window.open, my session values of parent(the screen that invoked the modal window) are lost. Please this is urgent.
9
2812
by: cendrizzi | last post by:
Hi all, I've read some stuff on this but can't seem to come up with a solution that works right. I have a semi-mature (yet very large and robust) internal web application that currently only works with one window open at a time. Maybe I'm abusing the use of $_SESSION but I have data entry processes split up in several steps (which is required since depending on what was put before determines what pages will be shown after). To store...
2
1457
by: John Derry | last post by:
I'm having a curious problem. I open a window with window.open method and supply a url which is a local file of file:/// form. After that, I go into the document of this new window to read the DOM for multiple forms which are defined in the html. The problem is that the objects are not immediately there. To make them available, I've found that if I do a View, Source from the new window's menu bar, THEN the DOM forms are there as expected....
5
1366
by: dlf | last post by:
Hello, I'm building a little web with php and javascript. I have inside HEAD the following function: This function receives as parameters a url and a string. In the url it changes "aaaa" chars, inserting the value received in the string. <script type="text/javascript"> function abrir(url,cadena){
5
4476
by: ziycon | last post by:
I have the below code and it works fine in FF but in IE 6&7 it wont work, gives an error to do with window.open?? <script type="text/javascript"> <!-- function radio(dirLevel) { window.open(dirLevel+"radio.html", "Radio", "status=1, height=120, width=450, resizable=0, top=20, left=20") } //--> </script>
0
9373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9227
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2590
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2143
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.