473,406 Members | 2,816 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.

Can 'onbeforeunload' (ie) be used to trigger a javascript

Can 'onbeforeunload' (ie) be used to trigger a javascript?

I can't find any info on how or if this is possible.

Regards.
Jul 23 '05 #1
2 3486


Mozzie ³ »( òvó )« »( òvó )« T wrote:
Can 'onbeforeunload' (ie) be used to trigger a javascript?

I can't find any info on how or if this is possible.


Yes, sure, the following inserts a warning message into the page with
both IE 6 and Mozilla 1.7 for me if I try to navigate away from the page
so script is executed:

<html>
<head>
<title>calling script in the beforeunload event handler</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function showWarning (message) {
var p;
if (document.createElement && (p = document.createElement('p'))) {
p.className = 'warning';
p.appendChild(document.createTextNode(message));
document.body.insertBefore(p, document.body.firstChild);
}
}

window.onbeforeunload = function (evt) {
showWarning('Are you sure you want to leave this page?');
if (!evt) {
evt = window.event;
}
if (evt) {
evt.returnValue = 'Are you sure you want to leave this page?';
return evt.returnValue;
}
}
</script>
<style type="text/css">
p.warning {
color: red;
background-color: white;
font-weight: bold;
font-size: 110%;
}
</style>
</head>
<body>
<p>
Visit
<a href="http://JavaScript.FAQTs.com/">JavaScript.FAQTs</a>.
</p>
</body>
</html>
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
"Martin Honnen" <ma*******@yahoo.de> wrote in message news:40********@olaf.komtel.net...
|
|
| Mozzie ³ »( òvó )« »( òvó )« T wrote:
|
| > Can 'onbeforeunload' (ie) be used to trigger a javascript?
| >
| > I can't find any info on how or if this is possible.
|
| Yes, sure, the following inserts a warning message into the page with
| both IE 6 and Mozilla 1.7 for me if I try to navigate away from the page
| so script is executed:
|
| <html>
| <head>
| <title>calling script in the beforeunload event handler</title>
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
| <script type="text/javascript">
| function showWarning (message) {
| var p;
| if (document.createElement && (p = document.createElement('p'))) {
| p.className = 'warning';
| p.appendChild(document.createTextNode(message));
| document.body.insertBefore(p, document.body.firstChild);
| }
| }
|
| window.onbeforeunload = function (evt) {
| showWarning('Are you sure you want to leave this page?');
| if (!evt) {
| evt = window.event;
| }
| if (evt) {
| evt.returnValue = 'Are you sure you want to leave this page?';
| return evt.returnValue;
| }
| }
| </script>
| <style type="text/css">
| p.warning {
| color: red;
| background-color: white;
| font-weight: bold;
| font-size: 110%;
| }
| </style>
| </head>
| <body>
| <p>
| Visit
| <a href="http://JavaScript.FAQTs.com/">JavaScript.FAQTs</a>.
| </p>
| </body>
| </html>
|
|
| --
|
| Martin Honnen
| http://JavaScript.FAQTs.com/
|

Thanks for your help :-)

Regards.
Jul 23 '05 #3

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

Similar topics

4
by: Yuk Cheng | last post by:
<<<start index.htm>>> <html> <head> <script> function perform(action){ } </script> </head>
4
by: lltaylor | last post by:
Hello, Currently I use an onbeforeunload method, to do call various tidy up methods when a user exits my web application - however I now need to add additional support, for users migrating to...
1
by: lee_youjin | last post by:
I needed to defect when the user is clicking the IE X button ( top right). so I could execute the cleanup function. I was able to detect it using onbeforeunload event. Now I need to call the...
1
by: nebulus | last post by:
I'm working on a web app that under normal circumstances will be happy with the Session_OnEnd event, but when a user leaves a page by closing the browser by either Alt+F4 or just hitting the "X",...
1
by: sirmoreno | last post by:
Hi, I want to confirm if the user wants to exist the page without any save. my code: <script language="javascript" type="text/javascript"> window.onbeforeunload = confirmExit; function...
1
Frinavale
by: Frinavale | last post by:
Introduction I've seen many questions asked about how to disable the browser's back button and in the past I've replied with "it's simply not possible". It's not a good idea to disable the back...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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...
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,...

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.