簡體   English   中英

我怎樣才能通過js保護我的index.html網站所以我只能在我的電腦上打開它?

[英]how can i protect my index.html website by js so i can only open it on my pc?

我想讓我的本地 static html 網站密碼用一些 js 保護,這樣當我打開我的本地 html 文件時,它帶有我自己的密碼,當我在我的電腦中填寫我的用戶名和我的密碼時只有當密碼正確時,我才按回車鍵打開我的 index.html 文件。 目前我的代碼是

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    </head>
    <body>
    <Form>
    <!-- user-id -->
    <input type="text">
    <!-- user-password -->
    <input type="password">

    <button onclick="window.open('./index.html')"> Enter
    </button>
    </Form>
    <script>
    //   pls-help-me-idk-how-to-code-js
    </script>
    </body>
    </html> `` 

這不是一個正確的方法。 您需要一個后端才能正確執行它。 但如果它只是為了玩,你可以有一個警告框並比較它們的值:如果正確,它會顯示。

首先,您必須在該輸入中添加一個 id 或 class 以便在腳本部分中對它們進行 select 。 之后,您可以對它們進行 select 並添加您想要的任何值。 如:

<form>
  <!-- user-id -->
  <input id="user" type="text" />
  <!-- user-password -->
  <input id="pass" type="password" />

  <button onclick="window.open('./index.html')">Enter</button>
</form>
<script>
  document.getElementById("user").value = "UsernameOrId";
  document.getElementById("pass").value = "SomePassword";
</script>

為了進行比較,您應該從數據庫或某些服務之類的地方獲取正確的密碼,但由於這純粹是為了學習目的,您可以在腳本中對其進行硬編碼以進行檢查。 因此,最終的解決方案可能與此類似:

<body>
<form>
  <!-- user-id -->
  <input id="user" type="text" />
  <!-- user-password -->
  <input id="pass" type="password" />

  <button id="btn">Enter</button>
</form>
<script>
  const myPass = "SomePassword";
  document.getElementById("user").value = "UsernameOrId"; // predefining the value simulating is saved and by default filled up
  document.getElementById("pass").value = myPass; // predefining the value simulating is saved and by default filled up
  const btn = document.getElementById("btn"); // getting the button to control its behavior on click event
  btn.addEventListener("click", function () {
    const passWhenClickingTheBtn = document.getElementById("pass").value;
    if (myPass === passWhenClickingTheBtn) {  // checking the value entered for pass
      window.open("./index.html");
    }
  });
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM