程式碼:
String URL = "https://localhost:8080/LoginTest/home.jsp";
location.href = URL; // 無效
location.href = "https://localhost:8080/LoginTest/home.jsp"; // 有效
JavaScript 中 變數必須先宣告。範例中 String URL 是 Java 語法,JS 應用 var、let 或 const。
var URL = "https://localhost:8080/LoginTest/home.jsp"; // ES5
// 或
let URL = "https://localhost:8080/LoginTest/home.jsp"; // ES6+
location.href = URL; // 正常跳轉
URL = "...")會建立全域變數(非嚴格模式),但屬於不良習慣。location.href 賦值會觸發頁面導向,確保 URL 字串正確編碼。習慣加上 let/const,避免變數提升與作用域污染。