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


当前回答

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

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

其他回答

这是很容易实现的,你可以使用:

window.location.href = "http://www.example.com/";

这将记住前一页的历史,所以可以通过点击浏览器的背面按钮返回。

或:

window.location.replace("http://www.example.com/");

此方法不记得前一页的历史,此情况下后按钮将被禁用。

<script type="text/javascript">
    if(window.location.href === "http://stackoverflow.com") {      
         window.location.replace("https://www.google.co.in/");
       }
</script>
var url = 'asdf.html';
window.location.href = url;

您可以使用它如下代码,在那里 getRequestToForwardPage 是请求地图(URL)。

function savePopUp(){
    $.blockUI();
    $.ajax({
        url:"GuestHouseProcessor?roomType="+$("#roomType").val(),
        data: $("#popForm").serialize(),
        dataType: "json",
        error: (function() {
            alert("Server Error");
            $.unblockUI();
    }),
    success: function(map) {
        $("#layer1").hide();
        $.unblockUI();
        window.location = "getRequestToForwardPage";
    }
});

这就是申请的相同背景。

如果您只想使用 jQuery 特定的代码,那么以下代码可以帮助:

 $(location).attr('href',"http://www.google.com");
 $jq(window).attr("location","http://www.google.com");
 $(location).prop('href',"http://www.google.com"); 

如果您想在同一应用程序中重新引导到路线

window.location.pathname = '/examplepath'

这将是走路的方式。