使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
与 jQuery 合作 :
$(window).attr("location", "http://google.fr");
其他回答
根据我的工作经验 JavaScript 更适合重新定位
它取决于您要如何更改地址。 如果您想要在用户历史中登录您的网站, 请使用window.location.href='ur website';
否则在不记录历史的情况下使用window.location.replace("your website");
.
使用 JavaScript :
方法1:
window.location.href="http://google.com";
方法2:
window.location.replace("http://google.com");
使用 j 查询 :
方法1:美元(地点)
$(location).attr('href', 'http://google.com');
方法2:可再使用功能
jQuery.fn.redirectTo = function(url){
window.location.href = url;
}
jQuery(window).redirectTo("http://google.com");
我只是补充另一种方式:
要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:
<script>
if(window.location.href == 'old_url')
{
window.location.href="new_url";
}
// Another URL redirect
if(window.location.href == 'old_url2')
{
window.location.href="new_url2";
}
</script>
以真实生活为例
<script>
if(window.location.href == 'https://old-site.com')
{
window.location.href="https://new-site.com";
}
// Another URL redirect
if(window.location.href == 'https://old-site.com/simple-post.html')
{
window.location.href="https://new-site.com/simple-post.html";
}
</script>
通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。
var url = 'asdf.html';
window.location.href = url;
使用 :
function redirect(a) {
location = a
}
并称之为:redirect([url]);
没必要href
之后location
,因为它是隐含的。
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 登录后,Laravel重定向回原始目的地
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类