Solved

How to close a window unconditionally - Javascript

Posted on 2002-07-22
9
1,361 Views
Last Modified: 2012-05-04
How to close a window unconditionally when opening another window.

I have the following script on my index.htm page. The assumption is that the user's screen is 800 resolution width, and an alternative index page called index1024.htm is loaded in a new window if the user's screen resolution width is detected as 1024.  This allows me to have a properly designed set of pages for 1024 resolution as well as for the 800 resolution screen.

<head>
<SCRIPT LANGUAGE="JavaScript1.2">
if (screen.height >= 768 && screen.width >= 1024)
{
window.open("index1024.htm","index1024","scrollbars=yes,resizable=yes,width=1024,height=768,toolbar=yes,location=yes,status=yes,menubar=yes");
}
</script>
</head>


Problem:  If and when the user closes the 1024 Window, the original 800 resolution window becomes visible from behind the 1024 window.  Is there any way that I can force this 800 resolution window to be closed automatically when the 1024 resolution window is opened?

I have tried putting the following code before the window.open line:

window.close("index.htm","index");

This works but not unconditionally.  It makes IE present the user with a question to answer, which I would rather did not happen: "The web page you are viewing is trying to close the window.  Do you want to close this window? Yes/No"


0
Comment
Question by:Jeeva
  • 2
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Accepted Solution

by:
DreamMaster earned 100 total points
ID: 7169482
No, you can only close windows that you made yourself. What you could do, is set the focus to the window that you opened so that the original window will not be visible anymore.

Oh and by the way...pay attention to the screen.height property...it is not properly detected in Netscape 4.08 (I know that it's hardly used anymore, but you could encouter the effect)

Max.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7169557
Why are you opening a new window?  You can just re-direct to the correct page with:
window.location='index1024.htm';

Cd&
0
 

Author Comment

by:Jeeva
ID: 7169662
Thanks DreamMaster, forgive my ignorance, but how do I focus on the new window?  Where do I insert the word focus or whatever in my window.open command, or do I need  a new command line, and if so what please?

Note to CobolDinooaur
You may be on to something here with your suggestion of not opening a new window, but I thought that I HAD to open  a new window, because what I want to do is ensure that my page is fully seen as I designed it, and is not surrounded by browser options, such as Favorites on the left of the IE browser screen.  Sorry I did not make that requirement clear in my question.

(I have the usual 2-frame page, with page links in the left frame.  I have tried using window.location='index1024.htm'; in the left frame and of course that does not replace my original 2-frame page, but just opens the new page replacing my left frame, leaving my old right frame still showing, so that I get a 3-frame page displayed).
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7169716
Oh its framed. Well then:

top.location='index1024.htm';

You have to load a new frameset.

As for things like favourites... What is the difference on a re-direct, and the original access to the page.  Whatever setup they had coming into the 800x600 page they have when you take them to the higher resolution.  You might also consider that some users do not like multiple windows being opened up, because they associate them with ad popups.

To help out with your query to Max you can set the focus this way:

x=window.open("index1024.htm","index1024","scrollbars=yes,resizable=yes,width=1024,height=768,toolbar=yes,location=yes,status=yes,menubar=yes");
x.focus();
}

Cd&
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 7169891
Even quicker solution would be to not even use javascript, but make the link have a target="_top" parameter...

And thanks Cd&, for the additional help while I was travelling... :-)

Max.
0
 

Author Comment

by:Jeeva
ID: 7170240
I have asked two related questions, (the other one being qid=20326341) concerning the same piece of code, and CobolDinosaur is also answering the second question, as well as assisting with this one.  I originally thought it would be easier to have two separate questions, than one complicated question, but that has proved not to be the case, since the answers above are partly dealing with my other question as well, and things are now in danger of getting confused.

So to simplify matters, I am awarding the points on this question to DreamMaster, since he has convinced me that it isn't possible to close the window, and I am continuing the discussion with CobolDinosaur on the second question.  CobolDinosaur's hints regarding using focus have not helped - the extra code seems to have no effect - more on this with my further comments on question 20326341 in due course.
0
 

Expert Comment

by:ColinPfeiffer
ID: 12261543
Sorry for offending anyone.   I was not trying to post a hack.  I work for a fairly large financial company. I am a developer building internal intranet sites for said company.   The code I posted is used quite a lot here on web server index pages.  The code detects the type of user then opens a new window to the appropriate site(hiding browser toolbars if nesessary) and then closes the index page.  We do not consider it a hack (maybe because we are using it on internal intranet sites).  Without the code I posted you get that warning message that the page is trying to close itself.  

The Business user clients here pushed the web team very hard to find a solution to stop that warning.  We will continue to use it here unless you have a better solution.  The business requirement is to make the redirect seemless (and also hide tool bars) we used  

window.open(strHref,'', 'toolbar=no,directories=no,resizable=yes');

to open the new window with no toolbars and then called the closeWindow logic that you removed to close the index page.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Uploading files to the web server has become common part of almost any kind of web application. People use different technologies to solve this, but regardless of the technology used, it is always useful to have some kind of progress indicator shown…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

9 Experts available now in Live!

Get 1:1 Help Now