使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
JavaScript 为您提供了许多获取和更改当前在浏览器地址栏中显示的 URL 的方法。 所有这些方法都使用位置对象, 这是窗口对象的属性。 您可以创建一个新的位置对象, 其当前 URL 如下 。
var currentLocation = window.location;
URL 基本结构
<protocol>//<hostname>:<port>/<pathname><search><hash>
协议 -- 指定协议名称用于访问互联网上的资源。 (HTTP(没有 SSL)或HTPS(没有 SSL))
主机名 -- -- 主机名指定拥有资源的主机名。例如,www.stackoverflow.com。一个服务器使用主机名提供服务。
端口 -- -- 用于识别因特网或其他网络电文到达服务器时要发送到的具体过程的端口号。
路径 - 路径为网络客户端想要访问的主机内的具体资源提供信息。 例如, stackoverflow. com/ index.html 。
查询 -- -- 查询字符串跟随路径组件,并提供一个资源可用于某种目的的信息字符串(例如,作为搜索的参数或作为要处理的数据)。
hash - URL 的锁定部分, 包括 hash 符号 (#) 。
使用这些位置对象属性,您可以访问所有这些 URL 组件
- 散数- 发送或返回 URL 的锁定部分。
- 主机主机- 发送或返回 URL 主机名和端口。
- 主机名- 发送或返回 URL 主机名。
- 小时( href)- 设置或返回整个 URL 。
- 路径名- set 或返回 URL 的路径名。
- 端口端口- set 或返回服务器用于 URL 的端口号。
- 议定书议定书- 发送或返回 URL 的协议。
- 搜索搜索- set 或返回 URL 的查询部分
如果您现在想要更改页面或将用户重定向到其他页面, 您可以使用该页面href
像这样的位置对象的属性
您可以使用位置对象的 href 属性。
window.location.href = "http://www.stackoverflow.com";
位置对象还有这三种方法
- (指定)- 装入新文档。
- 重新装入()- 重新装入当前文档。
- (替换)-- -- 将当前文档替换为新文档
您可以使用指派 () 并替换将重定向到其它页面的方法
location.assign("http://www.stackoverflow.com");
location.replace("http://www.stackoverflow.com");
转让 () 和替换 () 如何不同- 替换() 方法与指定() 方法() 之间的差别是,替换() 从文档历史中删除当前文档的 URL, 意思是无法使用“ 回溯” 按钮返回原始文档。所以,如果您想要装入新文档,则使用指定() 方法, 并想要给此选项返回原始文档 。
您可以使用jj 查询像这样
$(location).attr('href',url);
这样你就可以把用户 转到其他的URL上。
其他回答
标注描述 :
window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
杰克里:
var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
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>
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>
通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。
使用 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");
但如果有人想重回主页 他可能会使用下面的片段
window.location = window.location.host
如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。
您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台