如何使用javascript从一个页面转到另一个页面?

发布于 2024-10-07 07:05:22 字数 116 浏览 7 评论 0原文

如果用户有效,则浏览器应从管理页面转到另一个页面。

当用户输入用户名和密码后,单击“确定”按钮,然后显示另一个页面,并自动关闭登录页面。

我怎样才能用 JavaScript 做到这一点?

From an admin page if the user is valid then the browser should go to another page.

When a user enters his user name and password, then clicks the OK button then another page is displayed and the login page is automatically closed.

How can I do this with JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

野侃 2024-10-14 07:05:23

试试这个,

window.location.href="sample.html";

这里 sample.html 是下一页。它将转到下一页。

Try this,

window.location.href="sample.html";

Here sample.html is a next page. It will go to the next page.

徒留西风 2024-10-14 07:05:23

不能完全依赖客户端 JavaScript 来确定用户凭据是否正确。浏览器(以及执行该浏览器的所有代码)处于用户的控制之下,而不是您的控制之下,因此它不值得信任。

需要使用表格输入用户名和密码。 “确定”按钮将是一个提交按钮。 action 属性必须指向一个 URL,该 URL 将由检查凭据的程序处理。

该程序可以用 JavaScript 编写,但如何实现取决于服务器端 JavaScript 引擎< /a> 你正在使用。请注意,SSJS 不是主流技术,因此如果您确实想使用它,则必须使用专门的托管或管理自己的服务器。

(五年后,由于 Node.js,SSJS 变得更加常见,但它仍然相当专业)。

如果您想随后重定向,则程序需要发出 HTTP位置标头

请注意,在输出任何私有页面之前,您需要检查凭据是否正确(通常通过在 cookie 中存储令牌(不是实际密码))。否则任何人都可以通过知道 URL 来访问私人页面(从而绕过登录系统)。

You cannot sanely depend on client side JavaScript to determine if user credentials are correct. The browser (and all code that executes that) is under the control of the user, not you, so it is not trustworthy.

The username and password need to be entered using a form. The OK button will be a submit button. The action attribute must point to a URL which will be handled by a program that checks the credentials.

This program could be written in JavaScript, but how you go about that would depend on which server side JavaScript engine you were using. Note that SSJS is not a mainstream technology so if you really want to use it, you would have to use specialised hosting or admin your own server.

(Half a decade later and SSJS is much more common thanks to Node.js, it is still fairly specialised though).

If you want to redirect afterwards, then the program needs to emit an HTTP Location header.

Note that you need to check the credentials are OK (usually by storing a token, which isn't the actual password, in a cookie) before outputting any private page. Otherwise anyone could get to the private pages by knowing the URL (and thus bypassing the login system).

那伤。 2024-10-14 07:05:23

在 javascript 中验证用户是否是管理员会带来麻烦,因为 javascript 代码对任何人都是可见的。服务器应该在登录过程后区分管理员和普通用户,然后相应地生成新页面。

也许这不是您想要回答的问题:

window.location.href="<the page you are going to>";

Verifying that a user is an admin in javascript leads to trouble because javascript code is visible to anyone. The server is the one who should tell the difference between an admin and a regular user AFTER the login process and then generate the new page accordingly.

Maybe that's not what you are trying to do so to answer your question:

window.location.href="<the page you are going to>";
只有一腔孤勇 2024-10-14 07:05:23

还有几种重定向方法:

  • location.replace("http://example.com");
  • location.href = "http://example.com"
  • window.location = "http://example.com"
  • window.location.href = "http://example.com"

There are a few more ways to redirect:

  • location.replace("http://example.com");
  • location.href = "http://example.com"
  • window.location = "http://example.com"
  • window.location.href = "http://example.com"
岁月苍老的讽刺 2024-10-14 07:05:23

我得到的正确解决方案是

<html>
      <head>
        <script type="text/javascript" language="JavaScript">
                  function clickedButton()
            {

                window.location = 'new url'

            }
             </script>
      </head>

      <form name="login_form" method="post">
            ..................
            <input type="button" value="Login" onClick="clickedButton()"/>
      </form>
 </html>

这里新的网址在单引号内给出。

The correct solution that i get is

<html>
      <head>
        <script type="text/javascript" language="JavaScript">
                  function clickedButton()
            {

                window.location = 'new url'

            }
             </script>
      </head>

      <form name="login_form" method="post">
            ..................
            <input type="button" value="Login" onClick="clickedButton()"/>
      </form>
 </html>

Here the new url is given inside the single quote.

厌倦 2024-10-14 07:05:23

更简单的方法是 window.location.href = "http://example.com/new_url";

但是,如果您想使用 JavaScript 检查用户名和密码是否为空并将其发送到php 检查用户是否在数据库中。您可以按照此代码轻松完成此操作。

html表单-

<form name="myForm" onsubmit="return validateForm()" method="POST" action="login.php" >         
    <input type="text" name="username" id="username" />
    <input type="password" name="password" id="password" />
    <input type="submit" name="submitBt"value="LogIn" />
</form>

javascript验证-

function validateForm(){
    var uname = document.forms["myForm"]["username"].value;
    var pass = document.forms["myForm"]["password"].value;

    if((!isEmpty(uname, "Log In")) && (!isEmpty(pass, "Password"))){

        return true;
    }else{
        return false;
    }
}

function isEmpty(elemValue, field){
    if((elemValue == "") || (elemValue == null)){
        alert("you can not have "+field+" field empty");
        return true;
    }else{
        return false;
    }
}

使用php检查数据库中的用户是否

<?php
    $con = mysqli_connect("localhost","root","1234","users");

    if(mysqli_connect_errno()){
        echo "Couldn't connect ".mysqli_connect_error();
    }else{
        //echo "connection successful <br />";
    }

    $uname_tb = $_POST['username'];
    $pass_tb = $_POST['password'];

    $query ="SELECT * FROM user";
    $result = mysqli_query($con,$query);

    while($row = mysqli_fetch_array($result)){
        if(($row['username'] == $uname_tb) && ($row['password'] == $pass_tb)){
            echo "Login Successful";
            header('Location: dashbord.php');
            exit();
        }else{
            echo "You are not in our database".mysqli_connect_error();
        }
    }
    mysqli_close($con);
?>

Easier method is window.location.href = "http://example.com/new_url";

But what if you want to check if username and password whether empty or not using JavaScript and send it to the php to check whether user in the database. You can do this easily following this code.

html form -

<form name="myForm" onsubmit="return validateForm()" method="POST" action="login.php" >         
    <input type="text" name="username" id="username" />
    <input type="password" name="password" id="password" />
    <input type="submit" name="submitBt"value="LogIn" />
</form>

javascript validation-

function validateForm(){
    var uname = document.forms["myForm"]["username"].value;
    var pass = document.forms["myForm"]["password"].value;

    if((!isEmpty(uname, "Log In")) && (!isEmpty(pass, "Password"))){

        return true;
    }else{
        return false;
    }
}

function isEmpty(elemValue, field){
    if((elemValue == "") || (elemValue == null)){
        alert("you can not have "+field+" field empty");
        return true;
    }else{
        return false;
    }
}

check if user in the database using php

<?php
    $con = mysqli_connect("localhost","root","1234","users");

    if(mysqli_connect_errno()){
        echo "Couldn't connect ".mysqli_connect_error();
    }else{
        //echo "connection successful <br />";
    }

    $uname_tb = $_POST['username'];
    $pass_tb = $_POST['password'];

    $query ="SELECT * FROM user";
    $result = mysqli_query($con,$query);

    while($row = mysqli_fetch_array($result)){
        if(($row['username'] == $uname_tb) && ($row['password'] == $pass_tb)){
            echo "Login Successful";
            header('Location: dashbord.php');
            exit();
        }else{
            echo "You are not in our database".mysqli_connect_error();
        }
    }
    mysqli_close($con);
?>
洒一地阳光 2024-10-14 07:05:23

对于 MVC 开发人员,使用 javascript 重定向浏览器:

window.location.href = "@Url.Action("Action", "Controller")";

For MVC developers, to redirect a browser using javascript:

window.location.href = "@Url.Action("Action", "Controller")";
梦过后 2024-10-14 07:05:22

要使用 javascript 简单地重定向浏览器:

window.location.href = "http://example.com/new_url";

要重定向并提交表单(即登录详细信息),不需要 javascript:

<form action="/new_url" method="POST">
   <input name="username">
   <input type="password" name="password">
   <button type="submit">Submit</button>
</form>

To simply redirect a browser using javascript:

window.location.href = "http://example.com/new_url";

To redirect AND submit a form (i.e. login details), requires no javascript:

<form action="/new_url" method="POST">
   <input name="username">
   <input type="password" name="password">
   <button type="submit">Submit</button>
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文