您可能会遇到以下情况:单击URL到达页面X,但在内部将您定向到另一个页面Y,这可以通过页面重定向实现。
在客户端使用JavaScript进行页面重定向非常简单,要将您的网站访问者重定向到新页面,只需增加 window.location
<html>
<head>
<script type="text/javascript">
<!--
function Redirect() {
window.location="https://www.learnfk.com";
}
//-->
</script>
</head>
<body>
<p>Click the following button, you will be redirected to home page.</p>
<form>
<input type="button" value="Redirect Me" onclick="Redirect();" />
</form>
</body>
</html>
您可以在将网站访问者重定向到新页面之前向其显示适当的消息,这将需要一点时间延迟才能加载新页面,以下示例显示了如何实现相同的函数。这里 setTimeout()是内置的JavaScript函数,可用于在给定的时间间隔后执行另一个函数。
<html>
<head>
<script type="text/javascript">
<!--
function Redirect() {
window.location="https://www.learnfk.com";
}
document.write("You will be redirected to main page in 10 sec.");
setTimeout('Redirect()', 10000);
//-->
</script>
</head>
<body>
</body>
</html>
You will be redirected to learnfk.com main page in 10 seconds!
以下示例显示了如何根据访问者的浏览器将网站访问者重定向到其他页面。
<html>
<head>
<script type="text/javascript">
<!--
var browsername=navigator.appName;
if( browsername == "Netscape" ) {
window.location="http://www.location.com/ns.htm";
} else if ( browsername =="Microsoft Internet Explorer") {
window.location="http://www.location.com/ie.htm";
} else {
window.location="http://www.location.com/other.htm";
}
//-->
</script>
</head>
<body>
</body>
</html>