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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4426184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:21:48  来源:igfitidea点击:

How to go from one page to another page using javascript?

javascriptweb-applicationsweb-application-project

提问by SIVAKUMAR.J

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?

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

回答by David Tang

To simply redirect a browser using javascript:

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

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

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

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

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

回答by Quentin

You cannotsanely depend on client sideJavaScript 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.

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

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.

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

This program could be written in JavaScript, but how you go about that would depend on which server side JavaScript engineyou 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.

这个程序可以用 JavaScript 编写,但是你如何去做取决于你使用的是哪个服务器端 JavaScript 引擎。请注意,SSJS 不是主流技术,因此如果您真的想使用它,则必须使用专门的托管或管理您自己的服务器。

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

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

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

如果你想在之后重定向,那么程序需要发出一个HTTP Location 标头

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

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

回答by Mircea Nistor

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.

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

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

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

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

回答by Vinay Podili

Try this,

尝试这个,

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

Here sample.htmlis a next page. It will go to the next page.

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

回答by SIVAKUMAR.J

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.

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

回答by Blasanka

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

更简单的方法是 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.

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

html form -

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

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

check if user in the database using php

使用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);
?>

回答by Ifeanyilawrence

For MVC developers, to redirect a browser using javascript:

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

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

回答by Abdul Wahed

hope this would help:

希望这会有所帮助:

window.location.href = '/url_after_domain';

window.location.href = '/url_after_domain';

回答by Love Kumar

Try this:

尝试这个:

window.location.href = "http://PlaceYourUrl.com";