简体   繁体   English

如何使用 javascript(文件是本地文件)从一个 HTML 文件重定向到另一个文件?

[英]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.

相关问题 如何使用javascript将值从一个html页面传递到另一个html页面 - How can I pass values from one html page to another html page using javascript 如何使用JavaScript将值从一个HTML页面传递到另一个HTML页面? - How can I pass a value from one HTML page to another using JavaScript? 如何使用 JavaScript 将数据从一个 HTML 页面传递到另一个页面 - How can i pass data from one HTML page to another using JavaScript 如何使用JavaScript将列表组之类的数据从一个HTML页面传递到另一HTML页面? - How can I pass data like list group from one html page to another using JavaScript? 如何使用另一个Javascript文件中的函数? - How can I use a function from one Javascript file in another? 如何重用另一个JavaScript文件中的代码? - How can I reuse code from one JavaScript file in another? 我可以使用 javascript 在两个本地 html 文件之间进行通信吗? - Can I communicate between two local html files using javascript? 如何将用户从一个 url 重定向到另一个具有 JavaScript 操作(例如添加到购物车)的网站 - How can I redirect users from one url to another website that has a JavaScript action like add to cart 如何使用 javascript 导入本地 csv 文件以创建 html 表? - How can I import local csv files using javascript to create html table? 如何在JavaScript中从一个本地IP到另一个本地IP检索平面文本数据? - How can I retrieve plane text data from one local ip to another in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM