如何使用 window.location 通过 JavaScript 重定向到不同的 URL?
你可能遇到过这样的情况:点击一个 URL 访问页面 X 时,你却被内部定向到了另一个页面 Y。这是由于页面重定向造成的。
在客户端使用 JavaScript 进行页面重定向非常简单。只需在你的 head 部分添加以下一行,即可将你的网站访问者重定向到新页面 −
示例
你可以尝试运行以下代码,了解如何使用 window.location 重定向到另一个 URL。在此,我们将重定向到主页
<html>
<head>
<script>
<!--
function Redirect() {
window.location.assign("https://tutorialspoint.com");
}
//-->
</script>
</head>
<body>
<p>Click the following button, you will be redirected to home page.</p>
<input type = "button" value="Redirect Me" onclick="Redirect();" />
</body>
</html>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP