簡體   English   中英

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

[英]How can I redirect from one HTML file to another using javascript (the files are local)?

index.html 文件路徑:/Users/nixon/Documents/Website Development/Pers Website/index.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")
}

--- 2020 年 8 月 8 日更新---

嘗試將代碼更新為此,它仍然無法正常工作。 HTML:

button onclick="goToURL" id="login" type="button" class="btn btn-lg btn-dark">Login</button">

JS:

function goToURL() {
  window.open("Users/nixon/Documents/Website Development/Pers Website/loginpage.html")
}

控制台中沒有錯誤: https://gyazo.com/29a2084c082f66f943795ecfef3b3909

[1.0] 您的 onclick 需要括號。

[1.1] 你可以做一個事件監聽器。 這樣做,你會省略括號

[1.2] 如果您除了使用 javascript 加載新頁面之外沒有做任何其他事情,那么您不需要 javascript。 錨元素用於導航到網頁。

[1.3] replace() 是位置 object 的一部分。 不是 window。 而不是window.replace()它應該是window.location.replace()

[1.4] 我之前提到過replace ,但注意到這使您無法使用后退按鈕。 如果您改用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM