473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Is it possible?

Is it possible, as IE does in Windows Update, to ask onbeforeunload if
the user want really close window (or tab) clicking on close (X) of the
window?
This window isn't opened by window.open.

I think something like this:

<script type="text/javascript">
window.onbeforeunload = function(){
if (confirm('Really want to go away?')){
this.close();
} else {
return null
}
}
</script>

But it doesn't work.

Any help?

Thx
--
Fabri
("Sono più di 30 punti...ma è come se fossere 8 o 9....")
Jul 23 '05 #1
8 4551
On 07/06/2005 12:26, Fabri wrote:
Is it possible, as IE does in Windows Update, to ask onbeforeunload if
the user want really close window (or tab) clicking on close (X) of the
window?
Yes.

[snip]
window.onbeforeunload = function(){
if (confirm('Really want to go away?')){
this.close();
} else {
return null
}
}


You're expected to return a string that contains the prompt to be
displayed. IE handles the confirmation and closing itself.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
VK
All you need to do is to *cancel* the windowClose event. This will trig
the internal confirmation dialog (so you don't need and you cannot set
it up):

<script type="text/javascript">
window.onbeforeunload = function(e){
if (e != null) {
e.preventDefault();
}
else {
event.returnValue = false;
}
}
</script>

Jul 23 '05 #3
Michael Winter wrote:
window.onbeforeunload = function(){
if (confirm('Really want to go away?')){
this.close();
} else {
return null
}
}

You're expected to return a string that contains the prompt to be
displayed. IE handles the confirmation and closing itself.


Sorry Mike, I don't understand, u mean perhaps:

<script type="text/javascript">
window.onbeforeunload = function(){
var retVal = confirm('Really want to go away?');
if (retVal){
this.close();
} else {
return null
}
}
</script>

? or not?

Can u please give me a working example?

Regards.

--
Fabri
("Sono più di 30 punti...ma è come se fossere 8 o 9....")
Jul 23 '05 #4
VK wrote:
....

[snip]

Thx :-)

--
Fabri
("Sono più di 30 punti...ma è come se fossere 8 o 9....")
Jul 23 '05 #5
On 07/06/2005 12:52, Fabri wrote:
Michael Winter wrote:
You're expected to return a string that contains the prompt to be
displayed. [...]
Sorry Mike, I don't understand


[snip]
Can u please give me a working example?


Microsoft's documentation provides one, but...

As an attribute:

<body onbeforeunload="return 'This is the prompt text.';">

As an event property assignment:

window.onbeforeunload = function() {
return 'This is the prompt text.';
};

The documentation uses the proprietary event.returnValue property
instead of a return statement, but the result is the same.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Michael Winter wrote:
As an event property assignment:

window.onbeforeunload = function() {
return 'This is the prompt text.';
};

The documentation uses the proprietary event.returnValue property
instead of a return statement, but the result is the same.


Not quite. The return statement won't have the desired effect on IE, and
event.returnValue won't have any effect on Mozilla. To write code which
works on both you have to use both.

window.onbeforeunload = function(event) {
var msg = 'This is the prompt text.';
if (!event) event = window.event;
if (event) event.returnValue = msg;
return msg;
};

Also note that if you use Mozilla's addEventHandler method to add handler
function, Mozilla will ignore the return value totally (so the function
gets called, but nothing you can do will make it pop up the warning box).

Here's a longer example (but still self-contained) which implements a
warning box whenever you try to navigate away from a page with a modified
form.

The original is at
http://codespeak.net/svn/kupu/trunk/...eforeunload.js,
documentation at
http://codespeak.net/svn/kupu/trunk/...FOREUNLOAD.txt, license at
http://codespeak.net/svn/kupu/trunk/...oc/LICENSE.txt. Add a call to
window.onbeforeunload.tool.addForms() to enable checking.

/* BeforeUnload form processing */
if (!window.beforeunload) (function() {
var BeforeUnloadHandler = function() {
var self = this;

this.message = "Your form has not been saved. All changes you have
made will be lost";
if (window._) {
this.message = _("Your form has not been saved. All changes you
have made will be lost");
};
this.forms = [];
this.chkId = [];
this.chkType = new this.CheckType();
this.handlers = [this.isAnyFormChanged];
this.submitting = false;

this.execute = function(event) {
if (self.submitting) return;
if (!event) event = window.event;

for (var i = 0; i < self.handlers.length; i++) {
var fn = self.handlers[i];
var message = message || fn.apply(self);
}
if (message===true) message = self.message;
if (message===false) message = undefined;
if (event) event.returnValue = message;
return message;
}
this.execute.tool = this;
}
var Class = BeforeUnloadHandler.prototype;

// form checking code
Class.isAnyFormChanged = function() {
for (var i=0; i < this.forms.length; i++) {
var form = this.forms[i];
if (this.isElementChanged(form)) {
return true;
}
}
return false;
}
Class.addHandler = function(fn) {
this.handlers.push(fn);
}
Class.onsubmit = function() {
var tool = window.onbeforeunload && window.onbeforeunload.tool;
tool.submitting = true;
}
Class.addForm = function(form) {
for (var i = 0; i < this.forms.length; i++) {
if (this.forms[i]==form) return;
}
this.forms.push(form);
form.onsubmit = this.onsubmit;
var elements = form.getElementsByTagName('input');
for (var j = 0; j < elements.length; j++) {
var ele = elements[j];
if (ele.type=='hidden') {
ele.setAttribute('originalValue', ele.defaultValue);
}
}
}
Class.addForms = function() {
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (!element) continue;
if (element.tagName=='FORM') {
this.addForm(element);
}
else {
var forms = element.getElementsByTagName('form');
for (var j = 0; j < forms.length; j++) {
this.addForm(forms[j]);
}
}
}
}
Class.removeForms = function() {
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (!element) continue;
if (element.tagName=='FORM') {
for (var j = 0; j < arguments.length; j++) {
if (this.forms[j] == element) {
this.forms.splice(j--, 1);
element.onsubmit=null;
}
}
} else {
var forms = element.getElementsByTagName('form');
for (var j = 0; j < forms.length; j++) {
this.removeForms(forms[j]);
}
}
}
}

Class.CheckType = function() {};
var c = Class.CheckType.prototype;
c.checkbox = c.radio = function(ele) {
return ele.checked != ele.defaultChecked;
}
c.password = c.textarea = c.text = function(ele) {
return ele.value != ele.defaultValue;
}
// hidden: cannot tell on Mozilla without special treatment
c.hidden = function(ele) {
var orig = ele.getAttribute("originalValue");
return orig && (ele.value != orig);
}

c['select-one'] = function(ele) {
for (var i=0 ; i < ele.length; i++) {
var opt = ele.options[i];
if ( opt.selected != opt.defaultSelected) {
if (i===0 && opt.selected) continue; /* maybe no default */
return true;
}
}
return false;
}

c['select-multiple'] = function(ele) {
for (var i=0 ; i < ele.length; i++) {
var opt = ele.options[i];
if ( opt.selected != opt.defaultSelected) {
return true;
}
}
return false;
}

Class.chk_form = function(form) {
var elements = form.elements;
for (var i=0; i < elements.length; i++ ) {
var element = elements[i];
if (this.isElementChanged(element)) {
return true;
}
}
return false;
}

Class.isElementChanged = function(ele) {
var method = ele.id && this.chkId[ele.id];
if (!method && ele.type && ele.name)
method = this.chkType[ele.type];
if (!method && ele.tagName)
method = this['chk_'+ele.tagName.toLowerCase()];

return method? method.apply(this, [ele]) : false;
};

window.onbeforeunload = new BeforeUnloadHandler().execute;
})();

Jul 23 '05 #7
On 07/06/2005 13:48, Duncan Booth wrote:
Michael Winter wrote:


[snip]
The documentation uses the proprietary event.returnValue property
instead of a return statement, but the result is the same.


Not quite. [...] To write code which works on both you have to use both.


No you don't. IE accepts either form, but the documentation just happens
to use the returnValue property.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8
Michael Winter wrote:
On 07/06/2005 13:48, Duncan Booth wrote:
Michael Winter wrote:


[snip]
The documentation uses the proprietary event.returnValue property
instead of a return statement, but the result is the same.


Not quite. [...] To write code which works on both you have to use both.


No you don't. IE accepts either form, but the documentation just happens
to use the returnValue property.

[snip]


Yes, you are correct.

I mis-remembered, or else I spent so long wrestling with the event on
Mozilla that I probably just ended up confusing myself.
Jul 23 '05 #9

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

Similar topics

4
by: Julia Briggs | last post by:
I am struggling to create a PHP function that would take a specified image (JPG, GIF or PNG) from a link, and resize it down to a thumbnail so it will always fit in a 200x250 space. I am hoping...
36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
20
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to...
7
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). ...
2
by: Bhupesh Naik | last post by:
This is a query regarding my problem to make a spell and grammar check possible in text area of a web page. We have aspx pages which are used to construct letters. The browser based screens...
1
by: AAA | last post by:
hi, I'll explain fastly the program that i'm doing.. the computer asks me to enter the cardinal of a set X ( called "dimX" type integer)where X is a table of one dimension and then to fill it...
25
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
4
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate...
7
by: Robert S. | last post by:
Searching some time now for documents on this but still did not find anything about it: Is it possible to replace the entry screen of MS Office Access 2007 - that one presenting that default...
14
by: bjorklund.emil | last post by:
Hello pythonistas. I'm a newbie to pretty much both programming and Python. I have a task that involves writing a test script for every possible combination of preference settings for a software...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.