如何防止客户端刷新页面?

时间:2019-04-23 11:59:49

标签: javascript node.js block page-refresh

我创建了一个Node.js应用程序,但不希望客户端能够刷新页面或在刷新之前至少给他警告。

这是我的client.html代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">

     <script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/pusher/4.2.2/pusher.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.min.js"></script>
     <script src="mainClients.js"></script>


    <title>Clients</title>
</head>

<body onload="RunOnBeforeUnload()">



<div id ="main">

  <div class="container" id = "1">

    <h1 id="questionIndex"></h1>
    <p id="question"></p>
    <form id="vote-form">

    </form>

    <br><br>

  </div>

    <script type="text/javascript">

         let url = window.location.search;
         code = url.substr(url.indexOf("=")+1);
         generatePageContents('questionIndex', 'question', 'vote-form', 'http://localhost:5000/QueryQuestion', code, 1)
     </script>

     <script type="text/javascript">
        if(typeof(form) == 'undefined') {
            const form = document.getElementById('vote-form');
            vote(form, 'os', 'submitId', 'http://localhost:5000/poll');

        }else {
            vote(form, 'os', 'submitId', 'http://localhost:5000/poll');
        }
    </script>

<script type="text/javascript">
    questionListener('topic-change', 'topic-change', 'questionIndex', 'question', 'vote-form', 'http://localhost:5000/QueryQuestion', code);
</script>

<script language="javascript">
function RunOnBeforeUnload() {window.onbeforeunload = function(){ return '!'; } }


</script>

</div>

</body>

</html>

这里有人知道如何处理此任务吗?我认为这应该很容易,但是我在互联网上找不到任何内容

0 个答案:

没有答案