使用HTML弹出窗口

时间:2015-03-11 20:47:06

标签: javascript html onclick

单击该按钮时,我想询问一系列消息并为用户提供辅助确认选项。但是它没有用。

<html>
<body>
    <p>Close the page to trigger the onunload event.</p>
    <script type="text/javascript">
        var changes = false;        
        window.onbeforeunload = function() {
            if (changes)
            {
                var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
                if (confirm(message)) return true;
                else return false;
            }
        }
    </script>

    <button onClick='function;'> </input>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

只需在函数()中添加“e”,变量即可。

之前:window.onbeforeunload = function(){

之后:window.onbeforeunload = function(e){

<html>
<body>
    <p>Close the page to trigger the onunload event.</p>
    <script type="text/javascript">   
        window.onbeforeunload = function(e) {            
            var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
            if (confirm(message)) return true;
            else return false;
        }
    </script>

    <button onClick='window.close();'> </input>
</body>
</html>