我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
您可以使用它如下代码,在那里 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");
其他回答
使用 location.replace() 将重定向您,但没有保存前一页的历史。
但是,如果你想保持你的历史,你必须使用 location.href=//path。
例子:
// Form with steps
document.getElementById('#next').onclick = function() {
window.location.href='/step2' // Iteration of steps;
}
// Go to next step
document.getElementById('#back').onclick = function() {
window.history.back();
}
// Finish
document.getElementById('#finish').onclick = function() {
window.location.href = '/success';
}
// On success page
window.onload = function() {
setTimeout(function() {
window.location.replace('/home'); // I can't go back to success page by pressing the back button
},3000);
}
jQuery 不需要,你可以这样做:
window.open("URL","_self","","")
这是那么容易的!
启动 HTTP 请求的最佳方式是使用 document.loacation.href.replace(‘URL’)。
此分類上一篇: 你可以將延遲時間設定到你想要的任何地方:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Document Title</title>
<script type="text/javascript">
function delayer(delay) {
onLoad = setTimeout('window.location.href = "http://www.google.com/"', delay);
}
</script>
</head>
<body>
<script>
delayer(8000)
</script>
<div>You will be redirected in 8 seconds!</div>
</body>
</html>
在JavaScript中,您可以通过使用以下方式重新引导到一个特定的页面:
window.location.replace("http://www.test.com");
或
location.replace("http://www.test.com");
或
window.location.href = "http://www.test.com";
使用 jQuery:
$(window).attr("location","http://www.test.com");
在 PHP、HTML 或 jQuery 部分后写下下面的代码,如果在 PHP 或 HTML 部分中间,则使用 <script> 标签。
location.href = "http://google.com"