关闭浏览器时显示警告提示并关闭会话

时间:2015-06-17 06:30:00

标签: javascript php

在从其他网站重定向时显示网页,并且在此过程中URL会更改为指向其他端口号。

  1. 所以,我希望JavaScript能够检测到URL是否正确。
  2. 如果用户关闭浏览器/选项卡,则显示带有警告消息的警报框。
  3. 运行PHP脚本以关闭用户会话
  4. 我的代码是:

    if (window.location.href === "http://abc123.net.au:2048 ") {  
    $(function () {  
    try {  
    opera.setOverrideHistoryNavigationMode('compatible');  
    history.navigationMode = 'compatible';  
    } 
    catch (e) {
    }
    function OnBeforeUnload() {    
    $(window).onbeforeunload;  
    // Post to script that will log the user out  
    xmlhttp.open("POST", "../logscript.php", true);  
    xmlhttp.send();  
    }
    
    function ReturnMessage() {  
    return "Wait, by closing this session I will end your session. You will be required to log in again t access the site.";
    }
    function UnBindWindow() 
    {
    $(window).unbind('beforeunload', ReturnMessage);
    }
    
    $(window).bind('beforeunload', ReturnMessage);
    });
    } else {  
    document.write('<div>code is not working</div>')
    }
    

2 个答案:

答案 0 :(得分:0)

这是你的第二个答案

window.onbeforeunload = bunload;

function bunload(){
   dontleave="Are you sure you want to leave?";
    return dontleave;
 }

答案 1 :(得分:-1)

    if(window.location.pathname.indexOf("abc123.net.au")) {
    {
    //test
    alert("your url contains this abc123.net.au");
    }

    function UnLoadWindow() {
    return 'We strongly recommends NOT closing this window yet.'
    }

    window.onbeforeunload = UnLoadWindow;

    function OnBeforeUnload()
    {
    window.onbeforeunload;
    // Post to script that will log the user out
    xmlhttp=new XMLHttpRequest(); }
    xmlhttp.open("POST","../logscript.php",true);
    xmlhttp.send();
    alert("POSTING NOW");     

    function handleWindowClose()
    {
    if((window.event.clientX<0) || (window.event.clientY<0))
    {
    event.returnValue = "Wait, by closing this session I will end your session.";
    }  
    }
    }else{  
    }