我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
基本上,jQuery只是一个JavaScript框架,在这种情况下,你只能使用纯粹的JavaScript,所以在这种情况下,你有3个选项使用VanillaJavaScript:
使用位置替换,这将取代当前页面历史,这意味着无法使用后按钮返回原始页面。
window.location.replace("http://stackoverflow.com");
2) 使用位置指定,这将为您保存历史,并使用返回按钮,您可以返回原始页面:
window.location.assign("http://stackoverflow.com");
3)我建议使用上述方式之一,但这可能是使用纯粹的JavaScript的第三个选项:
window.location.href="http://stackoverflow.com";
您也可以在 jQuery 中编写一个函数来处理它,但不推荐,因为它只是一个线纯的JavaScript函数,如果您已经在窗口范围内,也可以使用上面的所有函数没有窗口,例如 window.location.replace(“http://stackoverflow.com”);可以是 location.replace(“http://stackoverflow.com”);
我在下面的图像中展示了所有这些:
此分類上一篇
其他回答
JavaScript 是非常广泛的. 如果你想跳到另一个页面,你有三个选项。
window.location.href='otherpage.com';
window.location.assign('otherpage.com');
//and...
window.location.replace('otherpage.com');
当你想移动到另一个页面,你可以使用其中一个,如果这是你的要求. 但是,所有三个选项都限于不同的情况。
如果你有兴趣了解更多关于这个概念,你可以通过更多。
window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.
<script type="text/javascript">
if(window.location.href === "http://stackoverflow.com") {
window.location.replace("https://www.google.co.in/");
}
</script>
您可以使用 jQuery 如下:
window.location = "http://yourdomain.com";
如果你只想要jQuery,那么你可以这样做:
$jq(window).attr("location","http://yourdomain.com");
您可以使用它如下代码,在那里 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.replace("http://www.example.com/");
替代() 方法不会在会议历史中保存起源页面,因此用户无法使用后按钮返回并重新转向。
但是,如果你想要一个与点击链接相同的效果,你应该去:
window.location.href = "http://www.example.com/";
在此情况下,浏览器返回按钮将是活跃的。