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

M

Mozzie ³

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

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

Regards.
 
M

Martin Honnen

Mozzie said:
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>
 
Þ

Þ Mozzie ³

|
|
| 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top