[英]How can I redirect from one HTML file to another using javascript (the files are local)?
index.html file path: /Users/nixon/Documents/Website Development/Pers Website/index.html index.html 文件路径:/Users/nixon/Documents/Website Development/Pers Website/index.html
loginpage.html path: /Users/nixon/Documents/Website Development/Pers Website/loginpage.html loginpage.html 路径:/Users/nixon/Documents/Website Development/Pers Website/loginpage.html
let loginButton = document.querySelector("#login")
loginButton.addEventListener('click', reDirectingLoginPage);
function reDirectingLoginPage() {
window.replace("/Users/nixon/Documents/Website Development/Pers Website/index.html")
}
---UPDATE AS OF 08/08/2020--- --- 2020 年 8 月 8 日更新---
Tried updating the code to this and it still didn't work.尝试将代码更新为此,它仍然无法正常工作。 HTML:
HTML:
button onclick="goToURL" id="login" type="button" class="btn btn-lg btn-dark">Login</button">
JS: JS:
function goToURL() {
window.open("Users/nixon/Documents/Website Development/Pers Website/loginpage.html")
}
No errors in console: https://gyazo.com/29a2084c082f66f943795ecfef3b3909控制台中没有错误: https://gyazo.com/29a2084c082f66f943795ecfef3b3909
[1.0] Your onclick needs parenthesis. [1.0] 您的 onclick 需要括号。
[1.1] You could do an event listener instead. [1.1] 你可以做一个事件监听器。 Doing it this way, you'll omit the parenthesis
这样做,你会省略括号
[1.2] If you aren't doing anything else other than loading a new page with the javascript, you don't need javascript for that. [1.2] 如果您除了使用 javascript 加载新页面之外没有做任何其他事情,那么您不需要 javascript。 anchor elements are meant for navigating to webpages.
锚元素用于导航到网页。
[1.3] replace() is a part of the location object. [1.3] replace() 是位置 object 的一部分。 Not the window.
不是 window。 Instead of
window.replace()
it should be window.location.replace()
而不是
window.replace()
它应该是window.location.replace()
[1.4] I had previously mentioned replace
, but noticed that this keeps you from being able to use the back button. [1.4] 我之前提到过
replace
,但注意到这使您无法使用后退按钮。 If you use assign
instead, the back button will work normally.如果您改用
assign
,后退按钮将正常工作。
let loginButton1 = document.querySelector("#login1"); let loginButton2 = document.querySelector("#login2"); loginButton2.addEventListener('click', reDirectingLoginPage); function reDirectingLoginPage() { // [1.3] and [1.4] // window.location.replace("http://example.com"); window.location.assign("http://example.com"); alert("these url assignments aren't working stackoverflow code snippets. This alert proves it's running. Take these to your project and they should work."); }
<.-- [1.0] --> <button id="login1" onclick="reDirectingLoginPage()">Login1</button> <.-- [1:1] --> <button id="login2">Login2</button> <.-- [1.2] --> <a href="http://example.com">Login3</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.