跳轉(zhuǎn)方法:1、使用“l(fā)ocation.href="指定頁面的URL";”語句跳轉(zhuǎn);2、使用“l(fā)ocation.replace("指定頁面的URL");”語句跳轉(zhuǎn);3、使用“l(fā)ocation.assign("指定頁面的URL");”語句跳轉(zhuǎn)。
推薦教程:《JavaScript視頻教程》
方法1:使用window.location.href方式進行跳轉(zhuǎn)
該種方法可以直接跳轉(zhuǎn)指定頁面。
<script type="text/javascript"> window.location.href = "https://www.php.cn"; </script>
window可省略,可簡寫為:
location.href="URL"
方法2:使用window.loction.replace方式進行跳轉(zhuǎn)
語法:
window.location.replace("url") //可簡寫為 location.replace("url")
將地址替換成新 url,該方法通過指定 URL 替換當前緩存在歷史里(客戶端)的項目,因此當使用 replace 方法之后,你不能通過"前進"和"后退"來訪問已經(jīng)被替換的URL,這個特點對于做一些過渡頁面非常有用!
示例
<script type="text/javascript"> window.location.replace("https://www.php.cn") </script>
方法3:使用location.assign方式進行跳轉(zhuǎn)
assign()方法加載一個新的文檔。
語法:
location.assign(URL)
示例:
<script type="text/javascript"> window.location.assign("https://www.php.cn") </script>
方法4:使用window.navigate方式進行跳轉(zhuǎn)
語法:
window.navigate("url");
navigate對象包含有關(guān)瀏覽器的信息,也可以作為頁面跳轉(zhuǎn),后面直接加要跳轉(zhuǎn)的地方。
示例:
<script language="javascript"> window.navigate("b.html"); </script>