如何使用 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.com/");
}
</script>
</body>
</html>输出
以上代码将生成以下输出 −

单击“重定向”按钮后,我们将被重定向到一个新站点 −

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP