Re: Can 'onbeforeunload ' (ie) be used to trigger a javascript
Mozzie ³ »( òvó )« »( òvó )« T wrote:
[color=blue]
> Can 'onbeforeunload ' (ie) be used to trigger a javascript?
>
> I can't find any info on how or if this is possible.[/color]
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>callin g 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.creat eElement && (p = document.create Element('p'))) {
p.className = 'warning';
p.appendChild(d ocument.createT extNode(message ));
document.body.i nsertBefore(p, document.body.f irstChild);
}
}
window.onbefore unload = function (evt) {
showWarning('Ar e 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.FAQT s.com/">JavaScript.FA QTs</a>.
</p>
</body>
</html>
Re: Can 'onbeforeunload ' (ie) be used to trigger a javascript
"Martin Honnen" <mahotrash@yaho o.de> wrote in message news:40f29bd8$1 @olaf.komtel.ne t...
|
|
| 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>callin g 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.creat eElement && (p = document.create Element('p'))) {
| p.className = 'warning';
| p.appendChild(d ocument.createT extNode(message ));
| document.body.i nsertBefore(p, document.body.f irstChild);
| }
| }
|
| window.onbefore unload = function (evt) {
| showWarning('Ar e 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.FAQT s.com/">JavaScript.FA QTs</a>.
| </p>
| </body>
| </html>
|
|
| --
|
| Martin Honnen
| http://JavaScript.FAQTs.com/
|
Comment