使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
我只是补充另一种方式:
要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:
<script>
if(window.location.href == 'old_url')
{
window.location.href="new_url";
}
// Another URL redirect
if(window.location.href == 'old_url2')
{
window.location.href="new_url2";
}
</script>
以真实生活为例
<script>
if(window.location.href == 'https://old-site.com')
{
window.location.href="https://new-site.com";
}
// Another URL redirect
if(window.location.href == 'https://old-site.com/simple-post.html')
{
window.location.href="https://new-site.com/simple-post.html";
}
</script>
通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。
其他回答
单页应用在同一申请路线内
window.location.pathname = '/stack';
JavaScript :
location.href = "http://stack.com";
window.location = "http://stack.com";
jj 查询 :
$(location).attr('href', "http://www.stack.com");
$(window).attr('location', "http://www.stack.com");
角形 4
import { Router } from '@angular/router';
export class NavtabComponent{
constructor(private router: Router) {
}
this.router.navigate(['bookings/taxi']);
}
与 jQuery 合作 :
$(window).attr("location", "http://google.fr");
您可以在没有jQuery 的情况下这样做 :
window.location = "http://yourdomain.com";
如果你只想要jQuery, 你可以做它喜欢:
$jq(window).attr("location","http://yourdomain.com");
您可以使用:
window.location.replace("http://www.example.com/");
缩略replace()
方法不会保存会话历史中的源页, 因此用户无法使用后端按钮返回, 然后再被重定向 。 注意: 在此情况下, 浏览器后端按钮将被禁用 。
但是,如果您想要与点击链接相同的效果,您应该选择:
window.location.href = "http://www.example.com/";
在此情况下, 浏览器后端按钮将激活 。
在单击功能上,只需添加:
window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
window.location.href = "http://www.google.com";
});