<script>
window.onunload = onClose;
function onClose(){
alert("close?");
}

解决方案 »

  1.   

    onunload不行啊,那样切换到别的页面的时候也会当成是关闭窗口处理。
    切换页面的时候不理睬,要关闭的时候才触发,怎么办?
      

  2.   

    onunload 事件详细资料
    SyntaxInline HTML <ELEMENT onunload = "handler" ... >  All platforms 
    Event property object.onunload = handler Microsoft?JScript?(compatible with ECMA 262 language specification) only 
    object.onunload = GetRef("handler") Microsoft?Visual Basic?Scripting Edition (VBScript) 5.0 or later only 
    Named script <SCRIPT FOR = object EVENT = onunload>  Internet Explorer only Event Object InformationBubbles No 
    Cancels No 
    To invoke Close the current browser window. 
    Navigate to another location by entering a new address or selecting a Favorite. 
    Click the Back, Forward, Refresh, or Home button. 
    Click on an anchor that refers the browser to another Web page. 
    Invoke the anchor click method. 
    Invoke the document write method. 
    Invoke the document open method. 
    Invoke the document close method. 
    Invoke the window close method. 
    Invoke the window open method, providing the possible value _self for the window name. 
    Invoke the window navigate or NavigateAndFind method. 
    Invoke the location replace method. 
    Invoke the location reload method. 
    Specify a new value for the location href property. 
    Submit a FORM to the address specified in the ACTION attribute via the INPUT_submit control, or invoke the submit method. 
     
    Default action Removes the object or document from the browser window.  
      

  3.   

    你可以查看X网站的源代码嘛,hehe
      

  4.   

    网页关闭时有两个事件被触发:onunload
     
    onbeforeunload
      

  5.   

    to AceZLB (猪小弟::UP无分()):非常不幸,你要求的功能无法实现!实际上,关闭浏览器窗口包含了两个步骤:1. 离开(跟离开当前页面去别的页面的“离开”完全相同)
    2. 关闭窗口对于第一步,系统以 onbeforeunload, onunload 通知页面脚本
    对于第二步,系统根本就不打招呼!因此,对 javascript 来说,“关闭”与“离开”根本就是一回事,无法区分