我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?


当前回答

有很多方法可以做到这一点。

// window.location
window.location.replace('http://www.example.com')
window.location.assign('http://www.example.com')
window.location.href = 'http://www.example.com'
document.location.href = '/path'

// window.history
window.history.back()
window.history.go(-1)

// window.navigate; ONLY for old versions of Internet Explorer
window.navigate('top.jsp')


// Probably no bueno
self.location = 'http://www.example.com';
top.location = 'http://www.example.com';

// jQuery
$(location).attr('href','http://www.example.com')
$(window).attr('location','http://www.example.com')
$(location).prop('href', 'http://www.example.com')

其他回答

与 jQuery 合作:

$(window).attr("location", "http://google.fr");

下面是将代码转向另一个页面,时间为10秒。

<script>
    function Redirect()
    {
        window.location="http://www.adarshkr.com";
    }

    document.write("You will be redirected to a new page in 10 seconds.");
    setTimeout('Redirect()', 10000);
</script>

你也可以这样做,点击一个按钮使用 location.assign:

<input type="button" value="Load new document" onclick="newPage()">
<script>
    function newPage() {
        window.location.assign("http://www.adarshkr.com")
    }
</script>

jQuery 代码重新引导页面或 URL

您甚至可以直接将 URL 转移到 attr() 方法,而不是使用变量。

第二条路

 window.location.href="http://stackoverflow.com";

你也可以编码如下(两者都是内部相同的):

window.location="http://stackoverflow.com";

第三种方式

第四条路

Location.assign() 方法将新文档上传到浏览器窗口中。

window.location.assign("http://stackoverflow.com"); 

第五条路

如 attr() 方法( jQuery 1.6 引入后)

var url = “http://stackoverflow.com”; $(位置)。prop('href', url);

我已经使用JavaScript的重定向功能(),它工作了。

<script type="text/javascript">
    $(function () {
        //It's similar to HTTP redirect
        window.location.replace("http://www.Technomark.in");

        //It's similar to clicking on a link
        window.location.href = "Http://www.Technomark.in";
    })
</script>

单页申请,在同一申请路线内

window.location.pathname = '/stack';

JavaScript 的内容:

location.href = "http://stack.com";
window.location = "http://stack.com";

jQuery:

$(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']);
}