在JavaScript中,location.href是一個屬性,用于獲取或設置當前頁面的URL。它有以下用法:
var url = location.href;
console.log(url);
location.href = "http://example.com";
location.href = "/path/to/page.html";
location.href = "#section1";
location.href = "?key=value";
location.href = "#section1?key=value";
location.href = "http://example.com";
請注意,使用location.href設置URL會導致頁面的重加載。