简体   繁体   English

我怎样才能通过js保护我的index.html网站所以我只能在我的电脑上打开它?

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

i want to make my local static html website password protected with some js, so that when i open my local html file in my pc, it comes with a form to fill up my user id and my own password, which i have saved and when i hit enter that should open my index.html file only when password is correct.我想让我的本地 static html 网站密码用一些 js 保护,这样当我打开我的本地 html 文件时,它带有我自己的密码,当我在我的电脑中填写我的用户名和我的密码时只有当密码正确时,我才按回车键打开我的 index.html 文件。 currently my code is目前我的代码是

    <!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> `` 

Its not a proper way to do it.这不是一个正确的方法。 You need a backend for that inorder to properly execute that.您需要一个后端才能正确执行它。 But if it just to play around you can have an alert box and just compare their value: if right it displays.但如果它只是为了玩,你可以有一个警告框并比较它们的值:如果正确,它会显示。

First, you have to add an id or a class to that inputs in order to select them within the script section.首先,您必须在该输入中添加一个 id 或 class 以便在脚本部分中对它们进行 select 。 Afterward, you can select them and add any value you want.之后,您可以对它们进行 select 并添加您想要的任何值。 Such as:如:

<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>

In order to compare, you should get the right password from somewhere like a database or some service, but since this is purely for learning purposes you can hardcode it in the script for checking.为了进行比较,您应该从数据库或某些服务之类的地方获取正确的密码,但由于这纯粹是为了学习目的,您可以在脚本中对其进行硬编码以进行检查。 So, the final solution can be similar to this:因此,最终的解决方案可能与此类似:

<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.

相关问题 如何将麦克风声音传输从我的电脑网站打开到其他电脑打开的网站? - how can i make microphone sound transfer from website open in my pc to websites open in others pc? 如何将index.html中的内容放入我的view.html? - How can I put content from my index.html into my view.html? Express JS:我还能吗 <scripts> 在我的index.html中 - Express JS: Can I still have <scripts> in my index.html 如何将我的 Index.html 中的按钮连接到我的 Unity 代码 - How can i connect Buttons from my Index.html to my Unity Code 我的Javascript Ajax请求可在Phonegap index.html中使用,但不能在其他任何页面中使用,如何解决此问题? - My Javascript Ajax request works in Phonegap index.html but not in any other pages, how can I fix this? 如何使用gulp-inject将文件的内容插入到index.html中? - How can I insert the contents of a file in my index.html with gulp-inject? 如何在 React 的 index.html 文件中正确包含 css 文件? - How can I properly include a css file in my index.html file in React? 我如何在没有Index.html或.php的情况下将WebApplication文件运行到服务器? - How can I run my WebApplication files to the server without an Index.html or.php? 如何将单独的 html 页面加载到我的主 index.html 中? 我想可以通过使用 jquery 选择元素 id 来完成 - How can I load a separate html page into my main index.html? I imagine it can be done by selecting an element id with jquery index.html 可以播放音频文件吗? 如果是这样,我如何实现它在后台自动播放? - Can index.html play an audio file? If so, How can I implement it to auto play in the back ground?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM