Javascript window.onbeforeunload未执行所需操作

Javascript window.onbeforeunload未执行所需操作,javascript,Javascript,如果用户通过关闭选项卡卸载屏幕,我尝试使用window.onbeforeunload方法删除一组会话变量。 以下是我的功能: <script type="text/javascript"> window.onbeforeunload = function (e) { return 'Are you sure you want to leave this page? You will lose any unsaved data.'; d

如果用户通过关闭选项卡卸载屏幕,我尝试使用window.onbeforeunload方法删除一组会话变量。 以下是我的功能:

    <script type="text/javascript">
    window.onbeforeunload = function (e) {
        return 'Are you sure you want to leave this page?  You will lose any unsaved data.';
        document.getElementById('<%=btnFinshed%>').click();
        return null;
    };  
</script>  

window.onbeforeunload=函数(e){
return“您确定要离开此页面吗?您将丢失所有未保存的数据。”;
document.getElementById(“”)。单击();
返回null;
};  
我添加了返回消息,只是想看看它是否在发射,但在那里它变得更加混乱。我没有显示我的消息,而是(在Mozilla上)得到了以下消息:

此页面要求您确认是否要离开-您输入的数据可能无法保存

他做了类似的事情

我的btnFinshed_单击没有触发,无论我是否将消息放入,后续保留需要删除的会话变量都会导致进一步的错误

有什么想法吗


嗯。我添加了另一个按钮和另一个javascript函数,只是为了测试这个document.getElementById命令。结果是document.getElementById肯定不起作用。下面是例行公事:

    <script  language="javascript" type="text/javascript">
       function testThis(e) {
           var element1 = document.getElementById('<%=btnFinished%>');
           if (element1 != null) {
               alert("element is found");
               //code to set the value variable.
               document.getElementById('<%=btnFinished%>').fireEvent("onclick");
           }
           else {
               alert("element is null");
           }
           return false;
       };
</script>  

    <asp:Button ID="Button1" runat="server" Text="X" OnClientClick="testThis()" />

    <asp:Button ID="btnFinished" runat="server" Text="Finished" onclick="btnFinshed_Click" CausesValidation="False" /> - i renamed this button too.

函数testThis(e){
var element1=document.getElementById(“”);
if(element1!=null){
警报(“发现元素”);
//设置值变量的代码。
document.getElementById(“”).fireEvent(“onclick”);
}
否则{
警报(“元素为空”);
}
返回false;
};
-我也重新命名了这个按钮。
结果-document.getElementById无效。元素显然在那里。身份证显然是正确的。javascript命令必须无效。

请尝试以下操作:

window.onbeforeunload = function (e) {
    var e = e || window.event,
        msg = 'Are you sure you want to leave this page?  You will lose any unsaved data.';
    if (e) {
        e.returnValue = msg;
    }
    return msg;
};

这没有任何区别。我更关心的是让我的按钮点击触发。请尝试返回“”以检查此警报值是否正确。顺便说一句,您确定这不仅仅是一个输入错误吗?btnFinished而不是btnFinished?不,该控件绝对称为btnFinished!好啊我添加了另一个按钮和另一个javascript函数,只是为了测试这个document.getElementById命令。结果是document.getElementById肯定不起作用。下面是例行公事: