By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,189 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,189 IT Pros & Developers. It's quick & easy.

closing pop up windows

P: n/a
I have the following script on all pop up windows,
would like to have this work on all newer browsers if
possible. Right now it only works on MSIE & Opera.
Not sure why. Any suggestions are appreciated.
Not concerned with the print part yet. TIA Rich

<SCRIPT language="jscript">
function CloseWindow() {
self.close();
}
function printpage() {
window.print();
}
</SCRIPT>

<a href="#" onclick="printpage()">Print</a>
| <a href="#" onclick="CloseWindow()">Close Window</a>
Feb 26 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Greetings,

You really only need to use the code window.close(); window is standard
for most DOM's so you should not have any issues with this. HOWEVER due
to most security features in standard browsers, this function, no
matter how you write it, will only work in a window that was launched
using javascript. In other words, if you open a new browser page and
access the URL that contains this script and have try to close the
window, it will do one of two things:

1. It will prompt an alert telling you that a script is trying to close
the window and it will ask if you that is OK.

2. It will ignore you script and do nothing.

The above only happen if you are in the PARENT window.

If you are launching a new window through javascript then it is assumed
you have permission to close the window and you will then be able to
close it just fine.

I hope this helps,

- Peter Schmalfeldt
Manifest Interactive

Feb 26 '06 #2

P: n/a
Manifest Interactive said the following on 2/26/2006 3:06 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Greetings,

You really only need to use the code window.close(); window is standard
for most DOM's so you should not have any issues with this. HOWEVER due
to most security features in standard browsers, this function, no
matter how you write it, will only work in a window that was launched
using javascript. In other words, if you open a new browser page and
access the URL that contains this script and have try to close the
window, it will do one of two things:
One of 3 things.
1. It will prompt an alert telling you that a script is trying to close
the window and it will ask if you that is OK.

2. It will ignore you script and do nothing.
3. It will close the window in 90% (if not more) of the browsers on the
web if you add one more line of code.
The above only happen if you are in the PARENT window.
Huh?
If you are launching a new window through javascript then it is assumed
you have permission to close the window and you will then be able to
close it just fine.
I can close it, most of the time, even if script didn't open it. At
least until IE7 is released anyway.
I hope this helps,


It didn't, only confused because of your lack of quoting what you were
replying to.

The Unconditional Truth will explain it, search the archives for it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Feb 26 '06 #3

P: n/a
Rich wrote :
I have the following script on all pop up windows,
would like to have this work on all newer browsers if
possible. Right now it only works on MSIE & Opera.
I've filed a bugreport on the usage of window.close() at Microsoft and
Opera when closing a non-javascript-initiated window: Microsoft fixed it
in IE7 beta 2 and you can expect Opera to fix it too in Opera 9. There
is absolutely no reason as to why a browser should honor a script
closing a non-javascript-initiated window without the user's explicit
consent. Any window can be closed by the user with Alt+F4; any window
with a titlebar can be closed by the user by clicking the close command
icon.
Mozilla goes a bit further with
dom.allow_scripts_to_close_windows
in its user settings.
Not sure why. Any suggestions are appreciated.
Here's one suggestion. Do not duplicate the normal browser commands, the
UI-based ones: because
- it's unneeded,
- it requires javascript support,
- it wrongly induce the users to trust websites more than their browser
commands and induce the users into not using their own browsers
- it does not induce users to get to know the commands of their own
browsers.

So:

- do not create close buttons or, worse, "close" links
- do not create go back buttons or, worse, "go back/history" links
- do not create increase/decrease font size buttons or, worse,
"increase/decrease font size" links (1)
- do not create print buttons or, worse, "print" links; with maybe only
1 exception if the link is to print a frame in a frameset...but even
there, I strongly oppose frames-based design to begin with and anyway,
the native UI for printing indicate how/which frame(s) can be printed.
(1): but you can show the users how to increase the font size. This is
what w3schools.com does at their site (see Larger text? link:
http://www.w3schools.com/largetext.htm )
and I think this is very good into helping users to get to know their
browser commands and get to know + trust their browser. Empowering the
users and letting them in control of the technology is a winning strategy.

Not concerned with the print part yet. TIA Rich

<SCRIPT language="jscript">
Language is deprecated; type is required. Best is to use
type="text/javascript"
function CloseWindow() {
self.close();
}
function printpage() {
window.print();
}
</SCRIPT>

<a href="#" onclick="printpage()">Print</a>
A pseudo-link which will break when javascript support is disabled or
inexistant; then it will confuse users and search indexing robots.
| <a href="#" onclick="CloseWindow()">Close Window</a>


Gérard
--
remove blah to email me
Feb 26 '06 #4

P: n/a
Rich wrote:
I have the following script on all pop up windows,
would like to have this work on all newer browsers if
possible. Right now it only works on MSIE & Opera.
Not sure why. Any suggestions are appreciated.
Not concerned with the print part yet. TIA Rich

<SCRIPT language="jscript">


The language attribute is deprecated, type is required. 'jscript'
scripts are ignored by Gecko (and likely other) browsers. Use:

<script type="text/javascript">
[...]
--
Rob
Feb 26 '06 #5

P: n/a
RobG wrote:
Rich wrote:
I have the following script on all pop up windows,
would like to have this work on all newer browsers if
possible. Right now it only works on MSIE & Opera.
Not sure why. Any suggestions are appreciated.
Not concerned with the print part yet. TIA Rich

<SCRIPT language="jscript">

The language attribute is deprecated, type is required. 'jscript'
scripts are ignored by Gecko (and likely other) browsers. Use:

<script type="text/javascript">
[...]


I thought that "jscript" might be part of the problem.
We have 12 calendars filled with word links that generate pop-ups.
This is the code for each link:
<a href="#" onclick="Pop_Detail(3576, 58, 2, 2006)">Klondike Derby</a><br>
(yeah, it's the Scouts)... So, earlier in the page is this code:
<SCRIPT language="javascript">
function Pop_Detail(EventID, ScheduleID, ThisMonth, ThisYear)
{
var newWin;
newWin =window.open("events_popup.asp?bmode=Detail&EventI D=" +
EventID + "&ScheduleID=" + ScheduleID,
"kid",'resizable=yes,scrollbars=yes,width=600,heig ht=400,toolbar=no')
}
</SCRIPT>

Part of a massive asp/database site purchased from MS-oriented vendor.
No cross browser specifications, or testing. Once in a while, we make
a case to fix something, but got no authority. Just trying to learn.
It works, just don't close on Gecko browsers. Thanks all, rich
Feb 27 '06 #6

P: n/a
Rich wrote:
RobG wrote:
Rich wrote:
I have the following script on all pop up windows,
would like to have this work on all newer browsers if
possible. Right now it only works on MSIE & Opera.
Not sure why. Any suggestions are appreciated.
Not concerned with the print part yet. TIA Rich

<SCRIPT language="jscript">
The language attribute is deprecated, type is required. 'jscript'
scripts are ignored by Gecko (and likely other) browsers. Use:

<script type="text/javascript">
[...]


I thought that "jscript" might be part of the problem.
We have 12 calendars filled with word links that generate pop-ups.
This is the code for each link:
<a href="#" onclick="Pop_Detail(3576, 58, 2, 2006)">Klondike Derby</a><br>


You might find it better to add "return false" to the end of the onclick
to stop the page scrolling back to the top when pop-up links are clicked:

<a href="#"
onclick="Pop_Detail(3576, 58, 2, 2006); return false;">Klondike
Derby</a><br>

(yeah, it's the Scouts)... So, earlier in the page is this code:
<SCRIPT language="javascript">
The language attribute is deprecated (and has been for over 6 years),
type is required (see above).

function Pop_Detail(EventID, ScheduleID, ThisMonth, ThisYear)
{
var newWin;
newWin =window.open("events_popup.asp?bmode=Detail&EventI D=" +
EventID + "&ScheduleID=" + ScheduleID,
"kid",'resizable=yes,scrollbars=yes,width=600,heig ht=400,toolbar=no')
}
</SCRIPT>
That should be OK, your problem is the script tag the pop-up which uses
'language=jscript'. Change it to type as suggested.

Part of a massive asp/database site purchased from MS-oriented vendor.
No cross browser specifications, or testing.
Unfortunately all to common.

Once in a while, we make
a case to fix something, but got no authority. Just trying to learn.
Good luck. :-)

It works, just don't close on Gecko browsers.


Replace the language attribute as suggested and it should.

--
Rob
Feb 27 '06 #7

P: n/a
Rich wrote :
I thought that "jscript" might be part of the problem.
We have 12 calendars filled with word links that generate pop-ups.
Do you really think this is the best way (popups) to implement your
calendar application?
This is the code for each link:
<a href="#" onclick="Pop_Detail(3576, 58, 2, 2006)">Klondike Derby</a><br>
(yeah, it's the Scouts)... So, earlier in the page is this code:
<SCRIPT language="javascript">
function Pop_Detail(EventID, ScheduleID, ThisMonth, ThisYear)
{
var newWin;
newWin =window.open("events_popup.asp?bmode=Detail&EventI D=" +
EventID + "&ScheduleID=" + ScheduleID,
"kid",'resizable=yes,scrollbars=yes,width=600,heig ht=400,toolbar=no')
}
</SCRIPT>


As written, the code above will have *several* problems in different
circumstances.

- for many reasons, newWin shouldn't be a local variable
- the link href shouldn't be #
- the link target should be used and identified as kid
- no detection of the presence of the window is done; no testing of the
current url is done; so your code must close any instance of newWin and
then reopen entirely all over again a new window in your
webpage/application instead

How to correct all this is explained at:

DOM:window.open
http://developer.mozilla.org/en/docs/DOM:window.open

Gérard
--
remove blah to email me
Feb 27 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.