頁面跳轉方法
阿新 • • 發佈:2019-01-11
一.點選跳轉
- onclick跳轉
(1) 設定window的location.href屬性
onclick="window.location.href='URL'"
onclick="location='URL'"
(2)呼叫window的open方法
onclick="window.open('URL','_blank');" // 在新視窗開啟
onclick="window.open('URL','_self');" // 覆蓋當前頁
2.a標籤跳轉
<a href="URL" target="_blank">Preface</a> // 在新視窗開啟
<a href="URL" target="_self">Preface</a> // 覆蓋當前頁,target屬性預設為_self,此處可省略
二.自動跳轉
(1) HTML實現
<head> <!-- 以下方式只是重新整理不跳轉到其他頁面 --> <meta http-equiv="refresh" content="10"> <!-- 以下方式定時轉到其他頁面 --> <meta http-equiv="refresh" content="5;url=hello.html"> </head>
優點:簡單
缺點:Struts Tiles中無法使用
(2)javascript的實現
<script language="javascript" type="text/javascript">
// 以下方式直接跳轉
window.location.href='hello.html';
// 以下方式定時跳轉
setTimeout("javascript:location.href='hello.html'", 5000);
</script>
優點:靈活,可以結合更多的其他功能
缺點:受到不同瀏覽器的影響
(3)結合了倒數的javascript實現
<span id="totalSecond">5</span>
<script language="javascript" type="text/javascript">
var second = document.getElementById('totalSecond').textContent;
if (navigator.appName.indexOf("Explorer") > -1) {
second = document.getElementById('totalSecond').innerText;
} else {
second = document.getElementById('totalSecond').textContent;
}
setInterval("redirect()", 1000);
function redirect() {
if (second < 0) {
location.href = 'hello.html';
} else {
if (navigator.appName.indexOf("Explorer") > -1) {
document.getElementById('totalSecond').innerText = second--; //支援IE
} else {
document.getElementById('totalSecond').textContent = second--; //支援Firefox
}
}
}
</script>
參考文章: