首页 > 解决方案 > 从 javascript 打开页面不起作用

问题描述

我正在尝试使用 登录js,我知道这不安全,仅适用于学校 - 他们需要它。

如果用户名和密码正确,我希望它打开我的照片页面,这不会发生。

据我所知,其他一切工作正常,它正确收集和比较数据,但不会打开新页面。我已经尝试过this.location.href诸如window.location和的变体location.replace

我也尝试过更改URL使用完整的 URLphotos.php

该函数是通过在 中的单击调用的HTML

<button type="submit" onclick="checkPassword()" class="button">Login</button>

这是 JavaScript:

function check() {   //onclick
    var username = "test";
    var password = "admin";
    var usernameEntered = document.getElementById("username").value;
    var passwordEntered = document.getElementById("password").value;

    if (usernameEntered == username && passwordEntered == password) {
        this.location.href = "http://localhost/photos-v4/photos.php";
    } else {
        alert("Incorrect username or password");
    }
}

标签: javascript

解决方案


这里有一些方法可以做到这一点

在新页面中打开

window.open('http://localhost/photos-v4/photos.php', '_blank').location;

在当前标签中打开

window.location.href = 'http://localhost/photos-v4/photos.php'

推荐阅读