javascript - 从 javascript 打开页面不起作用
问题描述
我正在尝试使用 登录js
,我知道这不安全,仅适用于学校 - 他们需要它。
如果用户名和密码正确,我希望它打开我的照片页面,这不会发生。
据我所知,其他一切工作正常,它正确收集和比较数据,但不会打开新页面。我已经尝试过this.location.href
诸如window.location
和的变体location.replace
。
我也尝试过更改URL
使用完整的 URL和photos.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");
}
}
解决方案
这里有一些方法可以做到这一点
在新页面中打开
window.open('http://localhost/photos-v4/photos.php', '_blank').location;
在当前标签中打开
window.location.href = 'http://localhost/photos-v4/photos.php'
推荐阅读
- typescript - 打字稿泛型、约束和文字类型
- amazon-web-services - AWS Step Functions - 在不发生迭代时利用空 Map 状态输出
- python - google 的 python 风格指南 - 属性说明
- arrays - 如何在 Kotlin 中从单个字符串创建字符串数组
- javascript - Socket.io - React:如何显示房间中连接的用户?
- python - Python 请求问题 self._store[key.lower()][1]
- java - 用递归方法填充字符串列表(递归填充方法)
- r - 无法使用 R 对数据进行分类
- c# - EPPlus 将工作表从 Workbook1 复制到 Workbook2
- python - 模型拟合/TypeError:“NoneType”对象不可调用