Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Wanted To Execute Java Script Code when browser close button is clicked

    Hi,

    I want to execute some java script code when I close the browser. The following code is executing in I.E. But I want to execute in Firefox and Netscape.


    <html>
    <head>
    <script language="JavaScript">

    function fun()
    {

    if((window.event.clientX<0) || (window.event.clientY<0))
    {

    alert("Inside if");

    }
    }

    </script>

    </head>
    <body onunload="fun()">

    </body>
    </html>


    Can any One Please help me.

    Thanks in advance

    Subbarao

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure, but you may be having trouble with the event procedure... Try:
    Code:
    <html>
    <head>
    <script language="JavaScript"  type="text/javascript">
    // It's better to use the type attribute than language
    
    function fun(e)
    {
    // Gecko browsers, I believe, pass in event param's; IE uses window.event
    if (!e) var e=window.event;
    if((e.clientX<0) || (e.clientY<0))
    {
    
    alert("Inside if");
    
    }
    }
    
    window.onload=fun;
    </script>
    
    </head>
    <body>
    
    </body>
    </html>
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    onbeforeunload is to be recognized both for IE and Moz. But Moz uses another type of event handling and other attribute (pageX instead of clientX). Even so, it woun't work, as the onbeforeunload is not implemented in Mozilla to bear the pageX or pageY property. It will return "undefined"
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Pyth007,

    The solution you gave is not working.
    Can you please suggest something else.

    Thanks and Regards,
    Subbarao

  • #5
    New Coder
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi KOR,

    I used pageX in Mozila, but it is always returning 0(Zero). You mentioned that it will return undefined, but it returning always 0(Zero) only in any case.

    Thanks and Regards,

    Subbarao

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Based on what Kor stated about clientX / pageX properties, your may try:
    Code:
    function fun(e)
    {
    // Gecko browsers, I believe, pass in event param's; IE uses window.event
    if (!e) var e=window.event;
    if(e.clientX)
    {
    if((e.clientX<0) || (e.clientY<0))
    {
    
    alert("Inside if");
    
    }
    }
    } else if (e.pageX) {
    if((e.pageX<0) || (e.pageY<0))
    {
    
    alert("Inside if");
    
    }
    }
    } else {
    alert('Browser / event not supported');
    }
    Again, this is untested; I'm not sure if onunload carries the pageX info. If not, it still may not work.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #7
    New Coder
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Pyth007,
    The code which u gave is not working.
    third alert statement ie 'Browser / event not supported' is executing in case of Mozila and Netscape.

    Thanks and Regards
    Subbarao

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Most browsers support clientX (position in browser window) and also pageX (position in web page). For more details on how these are handled incorrectly in some browsers see http://javascript.about.com/library/blmousepos.htm
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    New Coder
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi felgall,
    Firefox and mozila is not supporting pageX. Can you please write a sample code regarding this?

    Thanks in advance,
    Subbarao

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Oh, but they support. Except that unbeforeunload event seems not to.

    Example? Sure. Capture the mouse position onmousemove:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/JavaScript">
    var posx;var posy;
    function getMouse(e){
    posx=0;posy=0;
    var ev=(!e)?window.event:e;//IE:Moz
    if (ev.pageX){//Moz
    posx=ev.pageX+window.pageXOffset;
    posy=ev.pageY+window.pageYOffset;
    }
    else if(ev.clientX){//IE
    posx=ev.clientX+document.body.scrollLeft;
    posy=ev.clientY+document.body.scrollTop;
    }
    else{return false}//old browsers
    document.getElementById('mydiv').firstChild.data='X='+posx+' Y='+posy;
    }
    </script>
    </head>
    <body onmousemove="getMouse(event)">
    <div id="mydiv">&nbsp;</div>
    </body>
    </html>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    New Coder
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi KOR,
    I want to execute javascript code when browser is closed. This code won't work.

    subbarao

  • #12
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I keep telling you that the only crossbrowser event is onbeforeunload. Unfortunately, it looks like this event is implemented incompletely in Mozilla, thus it can not bear information about pageX and pageY. At least I tried and I failed to do so. So that, as far as I tried, your problem can not be solved in Mozilla.

    Maybe someone else has other ideeas. I have not.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #13
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    What do you want to do, in fact?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #14
    New Coder
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi KOR,

    Please observe below code. If you run the below code in IE, when you close the browser alert statement will execute. I want the same functionality in Mozila and Netscape. My Idea is when close the browser I want to execute some java script code.

    <html>
    <head>
    <script language="JavaScript">

    function fun()
    {

    if((window.event.clientX<0) || (window.event.clientY<0))
    {

    alert("Inside if");

    }
    }

    </script>

    </head>
    <body onunload="fun()">

    </body>
    </html>

  • #15
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    No, Sir, I would like to know your final aim as I don't really think you need an alert. What would be the use to alert those values?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •