我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
我也认为 location.replace(URL)是最好的方式,但如果你想通知搜索引擎你的重新引导(他们不分析JavaScript代码查看重新引导),你应该添加 rel="canonical" meta 标签到你的网站。
添加一个Noscript 部分,其中有一个 HTML 更新 meta 标签,也是一个很好的解决方案. 我建议您使用这个 JavaScript 重定向工具来创建重定向。
样品代码无延迟看起来如下:
<!-- Place this snippet right after opening the head tag to make it work properly -->
<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->
<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://yourdomain.example/"/>
<noscript>
<meta http-equiv="refresh" content="0;URL=https://yourdomain.example/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
var url = "https://yourdomain.example/";
if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
{
document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->
其他回答
我只是用另一个更新的jQuery方法更新这个荒谬:
var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);
var url = 'asdf.html';
window.location.href = url;
您可以使用 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");
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.
推荐文章
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?