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
    Fullerton, CA
    Member #
    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) ||
    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") ;;;'alpha(opacity='+opacity+')';;; "100%" "100%"'block';
    var message = document.getElementById("messageWindow"); ="visible";;
    message.innerHTML = "made it to the div display";
    r = confirm("made it the confirm");
    } else {'none';

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

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




  3. #2
    Senior Member
    Join Date
    Jun 2005
    Atlanta, GA
    Member #
    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:

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 12:01 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: