Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Senior Member shoffman3572's Avatar
    Join Date
    Jul 2008
    Location
    Fullerton, CA
    Posts
    137
    Member #
    16990
    Can anyone give me a clue as to how I can detect a user closing the browser using javascipt? I've been trying onunload and onBeforeUnLoad but the later doesn't seem to work anything but IE, and onUnLoad closes the browser before I can make a message (between <div> tags) visible...

    Here's the code I'm working with - I also don't want to use the 'confirm' function to stop the browser closing... and no pop ups - just to make content between div tags visible to the user..

    <script language="javascript">
    window.onbeforeunload = function()
    {
    if((window.event.clientX<0) ||
    (window.event.clientY<0))
    {
    grayOut(true);
    }
    }
    function grayOut(vis) {

    var options = options || {};
    var zindex = options.zindex || 50;
    var opacity = options.opacity || 70;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';

    if (vis) {
    var dark=document.getElementById("darkenScreenObject") ;
    dark.style.opacity=opaque;
    dark.style.MozOpacity=opaque;
    dark.style.filter='alpha(opacity='+opacity+')';
    dark.style.zIndex=zindex;
    dark.style.backgroundColor=bgcolor;
    dark.style.width= "100%"
    dark.style.height= "100%"
    dark.style.display='block';
    var message = document.getElementById("messageWindow");
    message.style.visibility ="visible";
    message.style.opacity=false;
    message.innerHTML = "made it to the div display";
    r = confirm("made it the confirm");
    } else {
    dark.style.display='none';
    }
    }
    </script>

    <style type="text/css">
    #darkenScreenObject {
    position:absolute; top:0px; left:0px; overflow:hidden; display:none; }

    #messageWindow {position:relative; margin: 50px auto; background-color:#FFFFFF; width:400px; height:300px; border:3px solid #000000;visibility:hidden; z-index:100;
    }

    </style>
    </head>
    <body topmargin="0" bottommargin="0">
    <p> web page content goes here..</p>
    <div id="messageWindow"></div>
    <div id="darkenScreenObject"></div>

    </body>
    </html>

    Thanks...

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Basically from what I can gather both onUnload and onBeforeUnload are relatively unreliable. I've tried to use them before with fairly mixed results. I'm also fairly sure that onBeforeUnload is the one to use for canceling browser page changes.

    The most important aspect of onBeforeUnload is that you can't do anything you want in it. What onBeforeUnload has to be is a single function that returns a string. That string is then displayed to the user in a confirmation dialog, and if they click ok then they leave the page, and if they click cancel then they stay on the page. That's it. The only thing your onbeforeunload function can do is return a String for confirmation.

    EDIT: See the MDC article on this: https://developer.mozilla.org/En/DOM...onbeforeunload


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:08 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com