如何使用 JavaScript 重定向到另一個網頁?


要使用 JavaScript 重定向到另一個網頁,程式碼如下所示 −

示例

 動態演示

<!DOCTYPE html>
<html>
<head>
<h1>Redirect to a Webpage Example</h1>
<button class="redirectBtn">Redirect</button>
<h2>Click the above button to Redirect to another Webpage</h2>
<script>
   document .querySelector(".redirectBtn") .addEventListener("click", redirectFunction);
   function redirectFunction() {
      location.href("https://tutorialspoint.tw/");
   }
</script>
</body>
</html>

輸出

以上程式碼將生成以下輸出 −

單擊“重定向”按鈕後,我們將被重定向到一個新站點 −

更新於: 12-5-2020

2 千次瀏覽

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告
© . All rights reserved.