如何使用 jQuery 将页面重定向到 URL
搜索引擎使用 301 状态代码将页面排名从旧 URL 转移到新 URL。
jQuery 重定向返回 http 响应状态代码:200 OK。
所以 jQuery 重定向和 Javascript 重定向一样,对搜索引擎不友好,最好使用其他返回 301 Moved Permanently 状态码的重定向方法。
jQuery 重定向实际上是Javascript 重定向。使用 jQuery 重定向被认为是矫枉过正,因为您可以 用更少的代码进行纯Javascript 重定向。
将带有重定向代码的旧页面替换为您要重定向到的页面的 URL。
旧页面.html:
<!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<脚本类型="text/javascript">
// jQuery URL 重定向
$(document).ready( function() {
url = "http://www.mydomain.com/new-page.html";
$( location ).attr( "href", url);
});
</script>
</body>
</html>
jquery-redirect-test.htm
<!DOCTYPE html>
<html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type=" text/javascript">
$(document).ready( function() {
url = "https://www.rapidtables.com/web/dev/jquery-redirect.htm";
$(location).attr("href" , 网址);
});
</script>
</body>
</html>
按此链接从jquery-redirect-test.htm 重定向回此页面: