473,608 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.onbefore unload = function(){
if (confirm('Reall y 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 4569
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.onbefore unload = function(){
if (confirm('Reall y 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.onbefore unload = function(e){
if (e != null) {
e.preventDefaul t();
}
else {
event.returnVal ue = false;
}
}
</script>

Jul 23 '05 #3
Michael Winter wrote:
window.onbefore unload = function(){
if (confirm('Reall y 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.onbefore unload = 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.onbefore unload = function() {
return 'This is the prompt text.';
};

The documentation uses the proprietary event.returnVal ue 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.onbefore unload = function() {
return 'This is the prompt text.';
};

The documentation uses the proprietary event.returnVal ue 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.returnVal ue won't have any effect on Mozilla. To write code which
works on both you have to use both.

window.onbefore unload = function(event) {
var msg = 'This is the prompt text.';
if (!event) event = window.event;
if (event) event.returnVal ue = 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.onbefore unload.tool.add Forms() to enable checking.

/* BeforeUnload form processing */
if (!window.before unload) (function() {
var BeforeUnloadHan dler = 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.isAnyFormC hanged];
this.submitting = false;

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

for (var i = 0; i < self.handlers.l ength; i++) {
var fn = self.handlers[i];
var message = message || fn.apply(self);
}
if (message===true ) message = self.message;
if (message===fals e) message = undefined;
if (event) event.returnVal ue = message;
return message;
}
this.execute.to ol = this;
}
var Class = BeforeUnloadHan dler.prototype;

// form checking code
Class.isAnyForm Changed = function() {
for (var i=0; i < this.forms.leng th; i++) {
var form = this.forms[i];
if (this.isElement Changed(form)) {
return true;
}
}
return false;
}
Class.addHandle r = function(fn) {
this.handlers.p ush(fn);
}
Class.onsubmit = function() {
var tool = window.onbefore unload && window.onbefore unload.tool;
tool.submitting = true;
}
Class.addForm = function(form) {
for (var i = 0; i < this.forms.leng th; i++) {
if (this.forms[i]==form) return;
}
this.forms.push (form);
form.onsubmit = this.onsubmit;
var elements = form.getElement sByTagName('inp ut');
for (var j = 0; j < elements.length ; j++) {
var ele = elements[j];
if (ele.type=='hid den') {
ele.setAttribut e('originalValu e', ele.defaultValu e);
}
}
}
Class.addForms = function() {
for (var i = 0; i < arguments.lengt h; i++) {
var element = arguments[i];
if (!element) continue;
if (element.tagNam e=='FORM') {
this.addForm(el ement);
}
else {
var forms = element.getElem entsByTagName(' form');
for (var j = 0; j < forms.length; j++) {
this.addForm(fo rms[j]);
}
}
}
}
Class.removeFor ms = function() {
for (var i = 0; i < arguments.lengt h; i++) {
var element = arguments[i];
if (!element) continue;
if (element.tagNam e=='FORM') {
for (var j = 0; j < arguments.lengt h; j++) {
if (this.forms[j] == element) {
this.forms.spli ce(j--, 1);
element.onsubmi t=null;
}
}
} else {
var forms = element.getElem entsByTagName(' form');
for (var j = 0; j < forms.length; j++) {
this.removeForm s(forms[j]);
}
}
}
}

Class.CheckType = function() {};
var c = Class.CheckType .prototype;
c.checkbox = c.radio = function(ele) {
return ele.checked != ele.defaultChec ked;
}
c.password = c.textarea = c.text = function(ele) {
return ele.value != ele.defaultValu e;
}
// hidden: cannot tell on Mozilla without special treatment
c.hidden = function(ele) {
var orig = ele.getAttribut e("originalValu e");
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.defaultSele cted) {
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.defaultSele cted) {
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.isElement Changed(element )) {
return true;
}
}
return false;
}

Class.isElement Changed = 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.tagN ame.toLowerCase ()];

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

window.onbefore unload = new BeforeUnloadHan dler().execute;
})();

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


[snip]
The documentation uses the proprietary event.returnVal ue 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.returnVal ue 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
14465
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 not to have it inserted or read from a database to do this function. Can it be done & someone please help me?
36
9450
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
2471
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 the site, and then i have to select the file to upload.. i used sendkeys.. and i worked perfect.. BUT ... the computer must be locked for security ( obviusly ) reazons.. so..i think this probable solutions to unlock the computer and run the...
7
2339
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). After some time, I added new function void f4(void).
2
3798
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 provide text area where the user can insert big chunks of text and submit it all to the server paragraph by paragraph. The requirement is to do a Spell Check AND Grammar Check in the text area. I did look at lot of possible third
1
6952
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 with numbers X; then the computer asks me how many subsets i have (nb_subset type (integer)) then,i have to enter for every sebset the card, and then to fill it, we'll have a two tables , one called cardY which contains nb_subset elements,and every...
25
2536
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. every 5 seconds, i just want to refresh it ONLY on server change just like desktop applications do. The problem is that refreshing evry n seconds has to much impact on my web server. The refresh action should be taken only when something
4
7672
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 a two dimensional array of all possible combinations of values. A simple example: 2 - columns and 2 possible values would generate: 0 0
7
3350
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 'templates' (with that big graphic buttons) - with some sort of own HTML-Page? I could imagine, that somehow it is possible to change this construction (hopefully not hardcoded in MS-Acc07), like it is possible to edit the 'Fluent Ribbon'? If so...
14
1995
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 I'm testing. I figured that this was something that a script could probably do pretty easily, given all the various possibilites. I started creating a dictionary of all the settings, where each key has a value that is a list of the possible...
0
8050
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7987
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8464
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
8130
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
8324
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6805
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...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1318
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.