使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
使用 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");
其他回答
使用 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");
我也认为location.replace(URL)
这是最好的方法, 但是如果您想要通知搜索引擎您的调整方向( 他们不分析 JavaScript 代码来查看调整方向) , 您应该添加rel="canonical"
网站的元标签 。
添加带有 HTML 刷新元标记的标注部分, 也是一个很好的解决方案 。 我建议您使用此JavaScript 调整方向工具以创建再定向。它也拥有互联网探索者支持以通过 HTTP 查询器 。
毫不延迟的样本代码看起来是这样的:
<!-- 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 -->
ECMAScript 6 + jQuery,85 字节
$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")
请不要杀我,这是个玩笑,这是个玩笑这是一个笑话。
这“为问题提供了答案”, 意思是它要求找到“使用jQuery”的解决方案,
Ferrybig显然需要解释笑话(还是开玩笑,
其他的答案是使用 jQuery 的attr()
会 议 日 程 和 议 程location
或window
不必要的物体。
这个答案也滥用了它, 但以一种更荒谬的方式。 它不是用它来设定位置, 而是用它来设置位置,attr()
获取设置位置的函数。
函数被命名jQueryCode
即使它没有任何jQuery, 并且号召一个函数somethingCode
特别是当事情甚至不是语言的时候。
“85字节”是指高尔夫代码。高尔夫显然不是你在高尔夫代码之外应该做的,此外,这个答案显然不是实际的高尔夫。
基本上, 畏缩。
从客户的侧面调整方向:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript and jQuery example to redirect a page or URL </title>
</head>
<body>
<div id="redirect">
<h2>Redirecting to another page</h2>
</div>
<script src="scripts/jquery-1.6.2.min.js"></script>
<script>
// JavaScript code to redirect a URL
window.location.replace("http://stackoverflow.com");
// window.location.replace('http://code.shouttoday.com');
// Another way to redirect page using JavaScript
// window.location.assign('http://code.shouttoday.com');
// window.location.href = 'http://code.shouttoday.com';
// document.location.href = '/relativePath';
//jQuery code to redirect a page or URL
$(document).ready(function(){
//var url = "http://code.shouttoday.com";
//$(location).attr('href',url);
// $(window).attr('location',url)
//$(location).prop('href', url)
});
</script>
</body>
</html>
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上。