Rya*_*ary 14393
jQuery不是必需的,window.location.replace(...)
最好模拟HTTP重定向.
window.location.replace(...)
比使用更好window.location.href
,因为replace()
不会将原始页面保留在会话历史记录中,这意味着用户不会陷入永无止境的后退按钮惨败.
如果您想模拟某人点击链接,请使用
location.href
如果要模拟HTTP重定向,请使用
location.replace
例如:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)
Bor*_*éry 1636
警告:此答案仅作为可能的解决方案提供; 它显然不是最好的解决方案,因为它需要jQuery.相反,更喜欢纯JavaScript解决方案.
$(location).attr('href', 'http://stackoverflow.com')
Run Code Online (Sandbox Code Playgroud)
Mar*_*.io 629
标准的"vanilla"JavaScript重定向页面的方式:
window
如果你在这里是因为你在重定向时丢失了 HTTP_REFERER,请继续阅读:
以下部分适用HTTP_REFERER
于许多安全措施之一(虽然它不是一个很好的保护措施).如果您使用的是Internet Explorer 8或更低版本,则在使用任何形式的JavaScript页面重定向(location.href等)时,这些变量都会丢失.
下面我们将实现IE8及更低版本的替代方案,以便我们不会丢失HTTP_REFERER.否则你几乎总是可以简单地使用
window.location.href
.
针对测试HTTP_REFERER
(URL粘贴,会议等),可以是讲述一个请求是否合法,有益的.
(注意:还有一些方法可以解决这些引用者的问题,如评论中的droop链接所述)
简单的跨浏览器测试解决方案(回退到Internet Explorer 9+和所有其他浏览器的window.location.href)
用法: redirect('anotherpage.aspx');
window.location.href = 'newPage.html';
Run Code Online (Sandbox Code Playgroud)
Gov*_*ngh 416
有很多方法可以做到这一点.
// window.location
window.location.replace('http://www.example.com')
window.location.assign('http://www.example.com')
window.location.href = 'http://www.example.com'
document.location.href = '/path'
// window.history
window.history.back()
window.history.go(-1)
// window.navigate; ONLY for old versions of Internet Explorer
window.navigate('top.jsp')
// Probably no bueno
self.location = 'http://www.example.com';
top.location = 'http://www.example.com';
// jQuery
$(location).attr('href','http://www.example.com')
$(window).attr('location','http://www.example.com')
$(location).prop('href', 'http://www.example.com')
Run Code Online (Sandbox Code Playgroud)
小智 320
这适用于每个浏览器:
window.location.href = 'your_url';
Run Code Online (Sandbox Code Playgroud)
tva*_*son 291
如果你对你想做的事情有一点描述性,那将会很有帮助.如果您尝试生成分页数据,则有一些选项可用于执行此操作.您可以为希望能够直接访问的每个页面生成单独的链接.
<a href='/path-to-page?page=1' class='pager-link'>1</a>
<a href='/path-to-page?page=2' class='pager-link'>2</a>
<span class='pager-link current-page'>3</a>
...
Run Code Online (Sandbox Code Playgroud)
请注意,示例中的当前页面在代码和CSS中的处理方式不同.
如果你想通过AJAX改变分页数据,这就是jQuery的用武之地.你要做的是为每个对应不同页面的锚标签添加一个点击处理程序.此单击处理程序将调用一些jQuery代码,通过AJAX获取下一页并使用新数据更新表.下面的示例假定您有一个返回新页面数据的Web服务.
$(document).ready( function() {
$('a.pager-link').click( function() {
var page = $(this).attr('href').split(/\?/)[1];
$.ajax({
type: 'POST',
url: '/path-to-service',
data: page,
success: function(content) {
$('#myTable').html(content); // replace
}
});
return false; // to stop link
});
});
Run Code Online (Sandbox Code Playgroud)
Pat*_*lán 249
我也认为这location.replace(URL)
是最好的方法,但是如果你想通知搜索引擎你的重定向(他们不分析JavaScript代码以查看重定向),你应该将rel="canonical"
meta标签添加到你的网站.
添加带有HTML刷新元标记的noscript部分也是一个很好的解决方案.我建议你使用这个JavaScript重定向工具来创建重定向.它还具有Internet Explorer支持以传递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.com/"/>
<noscript>
<meta http-equiv="refresh" content="0;URL=https://yourdomain.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
var url = "https://yourdomain.com/";
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 -->
Run Code Online (Sandbox Code Playgroud)
Nad*_*sin 223
但如果有人想重定向回主页,那么他可能会使用以下代码段.
window.location = window.location.host
Run Code Online (Sandbox Code Playgroud)
如果您有三个不同的环境,如开发,登台和生产,将会很有帮助.
只需将这些单词放在Chrome控制台或Firebug的控制台中,即可浏览此窗口或window.location对象.
Nik*_*wal 210
JavaScript为您提供了许多方法来检索和更改浏览器地址栏中显示的当前URL.所有这些方法都使用Location对象,它是Window对象的属性.您可以创建一个具有当前URL的新Location对象,如下所示.
var currentLocation = window.location;
Run Code Online (Sandbox Code Playgroud)
URL的基本结构
<protocol>//<hostname>:<port>/<pathname><search><hash>
Run Code Online (Sandbox Code Playgroud)
协议 - 指定用于访问Internet上资源的协议名称.(HTTP(不含SSL)或HTTPS(含SSL))
hostname - 主机名指定拥有该资源的主机.例如,www.stackoverflow.com.服务器使用主机名提供服务.
端口 - 用于识别Internet或其他网络消息到达服务器时要转发到的特定进程的端口号.
pathname - 该路径提供有关Web客户端要访问的主机中特定资源的信息.例如,stackoverflow.com/index.html.
查询 - 查询字符串如下路径组件,并且提供的信息,该资源可利用为了某种目的(例如,作为用于搜索的参数或作为要处理的数据)的字符串.
hash - URL的锚点部分,包括井号(#).
使用这些Location对象属性,您可以访问所有这些URL组件
现在,如果要更改页面或将用户重定向到其他页面,可以href
像这样使用Location对象的属性
您可以使用Location对象的href属性.
window.location.href = "http://www.stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)
Location Object也有这三种方法
您也可以使用assign()和replace方法重定向到这些其他页面
location.assign("http://www.stackoverflow.com");
location.replace("http://www.stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
assign()和replace()的区别如何 - replace()方法和assign()方法()之间的区别在于replace()从文档历史中删除当前文档的URL,意味着它不可能使用"后退"按钮导航回原始文档.因此,如果要加载新文档,请使用assign()方法,并希望选择导航回原始文档.
您可以通过改变位置对象的href属性的jQuery也是这样
$(location).attr('href',url);
Run Code Online (Sandbox Code Playgroud)
因此,您可以将用户重定向到其他网址.
Ali*_*eza 197
基本上jQuery只是一个JavaScript框架,在这种情况下做一些重定向等事情,你可以使用纯JavaScript,所以在这种情况下你有3个选项使用vanilla JavaScript:
1)使用位置替换,这将替换页面的当前历史记录,意味着无法使用后退按钮返回到原始页面.
window.location.replace("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
2)使用位置分配,这将保留您的历史记录,使用后退按钮,您可以返回到原始页面:
window.location.assign("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
3)我建议使用以前的方法之一,但这可能是使用纯JavaScript的第三个选项:
window.location.href="http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)
您也可以在jQuery中编写一个函数来处理它,但不推荐使用它,因为它只有一行纯JavaScript函数,如果您已经在窗口范围内,也可以使用所有上述函数而不使用窗口,例如window.location.replace("http://stackoverflow.com");
可以location.replace("http://stackoverflow.com");
我还会在下面的图片中显示所有内容:
小智 192
应该只能设置使用window.location
.
例:
window.location = "https://stackoverflow.com/";
Run Code Online (Sandbox Code Playgroud)
这是关于这个主题的过去的帖子:
Pat*_*hon 166
在开始之前,jQuery是一个用于DOM操作的JavaScript库.所以你不应该使用jQuery进行页面重定向.
来自Jquery.com的报价:
虽然jQuery可能在较旧的浏览器版本中没有出现重大问题,但我们并不主动测试它们中的jQuery,并且通常不会修复可能出现在它们中的错误.
它在这里找到:https: //jquery.com/browser-support/
因此,jQuery并不是一种能够实现向后兼容的最终解决方案.
以下使用原始JavaScript的解决方案适用于所有浏览器,并且已经标准很长时间,因此您不需要任何库来支持跨浏览器.
此页面将在3000毫秒后重定向到Google
<!DOCTYPE html>
<html>
<head>
<title>example</title>
</head>
<body>
<p>You will be redirected to google shortly.</p>
<script>
setTimeout(function(){
window.location.href="http://www.google.com"; // The URL that will be redirected too.
}, 3000); // The bigger the number the longer the delay.
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
不同的选项如下:
window.location.href="url"; // Simulates normal navigation to a new page
window.location.replace("url"); // Removes current URL from history and replaces it with a new URL
window.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL
window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click
window.navigate("page.html"); // Same as window.location="url"
Run Code Online (Sandbox Code Playgroud)
使用替换时,后退按钮不会返回到重定向页面,就好像它从未在历史记录中一样.如果您希望用户能够返回重定向页面,请使用window.location.href
或window.location.assign
.如果您确实使用了允许用户返回重定向页面的选项,请记住,当您进入重定向页面时,它会将您重定向回来.因此,在为重定向选择选项时要考虑到这一点.在页面仅在用户完成操作时重定向的情况下,将页面置于后退按钮历史记录中是可以的.但是,如果页面自动重定向,那么您应该使用替换,以便用户可以使用后退按钮而不会强制返回到重定向发送的页面.
您还可以使用元数据来运行页面重定向,如下所示.
META刷新
<meta http-equiv="refresh" content="0;url=http://evil.com/" />
Run Code Online (Sandbox Code Playgroud)
META位置
<meta http-equiv="location" content="URL=http://evil.com" />
Run Code Online (Sandbox Code Playgroud)
基地劫持
<base href="http://evil.com/" />
Run Code Online (Sandbox Code Playgroud)
可以在此页面上找到更多将您的毫无疑问的客户端重定向到他们可能不希望访问的页面的方法(其中一个不依赖于jQuery):
https://code.google.com/p/html5security/wiki/RedirectionMethods
我还想指出,人们不喜欢被随机重定向.仅在绝对需要时重定向人员.如果您开始随机重定向人员,他们将永远不会再次访问您的网站.
下一部分是假设的:
您也可能被报告为恶意网站.如果发生这种情况,那么当用户点击指向您网站的链接时,用户浏览器可能会警告他们您的网站是恶意的.如果人们报告您网站上的不良体验,搜索引擎可能会开始降低您的评分.
请查看有关重定向的Google网站站长指南:https: //support.google.com/webmasters/answer/2721217?hl = zh-CN&ref_topic = 6001971
这是一个有趣的小页面,可以让你离开页面.
<!DOCTYPE html>
<html>
<head>
<title>Go Away</title>
</head>
<body>
<h1>Go Away</h1>
<script>
setTimeout(function(){
window.history.back();
}, 3000);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果将两个页面示例组合在一起,您将拥有一个婴儿循环重新路由,这将保证您的用户永远不会再想要再次使用您的网站.
小智 141
这适用于jQuery:
$(window).attr("location", "http://google.fr");
Run Code Online (Sandbox Code Playgroud)
Sco*_*ion 141
你可以在没有jQuery的情况下做到这一点:
window.location = "http://yourdomain.com";
Run Code Online (Sandbox Code Playgroud)
如果你只想要jQuery那么你可以这样做:
$jq(window).attr("location","http://yourdomain.com");
Run Code Online (Sandbox Code Playgroud)
Sak*_*hik 88
#HTML页面重定向使用jQuery/JavaScript
试试这个示例代码:
function YourJavaScriptFunction()
{
var i = $('#login').val();
if (i == 'login')
window.location = "login.php";
else
window.location = "Logout.php";
}
Run Code Online (Sandbox Code Playgroud)
如果您想提供完整的URL window.location = "www.google.co.in";
.
Ash*_*tan 79
您需要在代码中添加以下行:
$(location).attr("href","http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
如果您没有jQuery,请使用以下命令运行JavaScript:
window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
iCo*_*nor 77
那么,问题是如何制作重定向页面,而不是如何重定向到网站?
您只需要使用JavaScript即可.这是一些将创建动态重定向页面的小代码.
<script>
var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
if( url ) window.location.replace(url);
</script>
Run Code Online (Sandbox Code Playgroud)
所以说你只需把这个片段放到redirect/index.html
你网站上的文件中就可以这样使用它.
http://www.mywebsite.com/redirect?url=http://stackoverflow.com
如果您转到该链接,它将自动将您重定向到stackoverflow.com.
这就是你用JavaScript 创建一个简单的重定向页面的方法
编辑:
还有一点需要注意.我添加window.location.replace
了我的代码,因为我认为它适合重定向页面,但是,你必须知道在使用时window.location.replace
你被重定向,当你按下浏览器中的后退按钮时它将不会返回到重定向页面,它将会回到它之前的页面,看看这个小小的演示事物.
例:
过程:store home => redirect page to google => google
在google:google => 后退按钮在浏览器 => 存储主页
所以,如果这符合您的需求,那么一切都应该没问题.如果要在浏览器历史记录中包含重定向页面,请将其替换为
if( url ) window.location.replace(url);
Run Code Online (Sandbox Code Playgroud)
同
if( url ) window.location.href = url;
Run Code Online (Sandbox Code Playgroud)
Ser*_*ect 75
*****原始问题是 - "如何重新使用JQUERY",HANS THE ANSWER IMPLEMENTS JQUERY >>免费使用案例*****
要使用JavaScript重定向到某个页面:
window.location.href = "/contact/";
Run Code Online (Sandbox Code Playgroud)
或者如果你需要延迟:
setTimeout(function () {
window.location.href = "/contact/";
}, 2000); // Time in milliseconds
Run Code Online (Sandbox Code Playgroud)
jQuery允许您轻松地从网页中选择元素.您可以在页面上找到任何想要的内容,然后使用jQuery添加特殊效果,对用户操作做出反应,或者显示和隐藏您选择的元素内部或外部的内容.所有这些任务都从知道如何选择元素或事件开始.
$('a,img').on('click',function(e){
e.preventDefault();
$(this).animate({
opacity: 0 //Put some CSS animation here
}, 500);
setTimeout(function(){
// OK, finished jQuery staff, let's go redirect
window.location.href = "/contact/";
},500);
});
Run Code Online (Sandbox Code Playgroud)
想象一下有人写了一行10000行的脚本/插件?!好吧,使用jQuery,您只需一两行即可连接到此代码.
Swa*_*rks 72
在您的点击功能上,只需添加:
window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
window.location.href = "http://www.google.com";
});
Run Code Online (Sandbox Code Playgroud)
til*_*lak 61
试试这个:
location.assign("http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
May*_*nty 56
不需要jQuery.你可以这样做:
window.open("URL","_self","","")
Run Code Online (Sandbox Code Playgroud)
就这么简单!
启动HTTP请求的最佳方法是使用document.loacation.href.replace('URL')
.
Anu*_*nup 52
先写好.您希望在应用程序中导航,从您的应用程序中为另一个链接导航.这是代码:
window.location.href = "http://www.google.com";
Run Code Online (Sandbox Code Playgroud)
如果你想在你的应用程序中导航页面,那么我也有代码,如果你想.
Aza*_*lvi 52
您可以像这样在jQuery中重定向:
$(location).attr('href', 'http://yourPage.com/');
Run Code Online (Sandbox Code Playgroud)
小智 45
在JavaScript和jQuery中,我们可以使用以下代码将一个页面重定向到另一个页面:
window.location.href="http://google.com";
window.location.replace("page1.html");
Run Code Online (Sandbox Code Playgroud)
lal*_*mar 45
使用Javascript:
window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
Run Code Online (Sandbox Code Playgroud)
jQuery的:
var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
Run Code Online (Sandbox Code Playgroud)
1j0*_*j01 45
$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")
Run Code Online (Sandbox Code Playgroud)
请不要杀我,这是个玩笑.这是个玩笑.这是个玩笑.
这确实"提供了问题的答案",因为它要求"使用jQuery"的解决方案,在这种情况下需要以某种方式强制它进入等式.
Ferrybig显然需要解释这个笑话(仍在开玩笑,我确信评论表上的选项有限),所以不用多说:
其他答案使用jQuery的attr()
的location
或window
不必要的对象.
这个答案也滥用它,但是更加荒谬.它不是用它来设置位置,而是attr()
用来检索设置位置的函数.
jQueryCode
虽然没有关于它的jQuery,但函数被命名,并且调用函数somethingCode
是非常可怕的,特别是当某些东西甚至不是一种语言时.
"85字节"是对Code Golf的引用.高尔夫显然不是你应该在代码高尔夫之外做的事情,而且这个答案显然不是高尔夫球.
基本上,畏缩.
Ste*_*ald 41
这是一个延时重定向.您可以将延迟时间设置为您想要的任何值:
<!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>
Run Code Online (Sandbox Code Playgroud)
Ben*_*Ben 38
有三种主要方法可以做到这一点,
window.location.href='blaah.com';
window.location.assign('blaah.com');
Run Code Online (Sandbox Code Playgroud)
和...
window.location.replace('blaah.com');
Run Code Online (Sandbox Code Playgroud)
对于传统的重定向,最后一个是最好的,因为它不会保存您在搜索历史记录中重定向之前所访问的页面.但是,如果您只想打开带有JavaScript的选项卡,则可以使用上述任何一个选项卡.1
编辑:window
前缀是可选的.
Epi*_*any 38
我只需要用另一个更新的jQuery方法来更新这种荒谬:
var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);
Run Code Online (Sandbox Code Playgroud)
Bid*_*yut 35
在PHP,HTML或jQuery部分之后编写以下代码.如果在PHP或HTML部分的中间,则使用<script>标记.
location.href = "http://google.com"
Run Code Online (Sandbox Code Playgroud)
Div*_*iya 34
第一道路
这是用于重定向页面的jQuery代码.因为,我把这个代码放在$(document).ready()函数上,它会在页面加载后立即执行.
var url = "http://stackoverflow.com";
$(location).attr('href',url);
Run Code Online (Sandbox Code Playgroud)
您甚至可以将URL直接传递给attr()方法,而不是使用变量.
第二种方式
window.location.href="http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)
你也可以像这样编码(内部都相同):
window.location="http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)
如果你对window.location和之间的区别感到好奇window.location.href
,那么你可以看到后一个是href
显式设置属性,而前一个是隐式地设置属性.由于window.location
返回一个对象,默认情况下会设置其.href
属性.
第三种方式
还有另一种使用JavaScript replace()
(window.location
对象方法)重定向页面的方法.您可以将新URL传递给该replace()
方法,它将模拟HTTP重定向.顺便说一句,请记住,该window.location.replace()
方法不会将原始页面放在会话历史记录中,这可能会影响后退按钮的行为.有时候,这就是你想要的,所以要小心使用它.
// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
第四种方式
喜欢attr()方法(在jQuery 1.6引入之后)
var url = "http://stackoverflow.com";
$(location).prop('href', url);
Run Code Online (Sandbox Code Playgroud)
Kal*_*hal 32
window.location.href="http://google.com";
Run Code Online (Sandbox Code Playgroud)
window.location.replace("http://google.com");
Run Code Online (Sandbox Code Playgroud)
$(location).attr('href', 'http://google.com');
Run Code Online (Sandbox Code Playgroud)
jQuery.fn.redirectTo = function(url){
window.location.href = url;
}
jQuery(window).redirectTo("http://google.com");
Run Code Online (Sandbox Code Playgroud)
Inc*_*nnu 29
在jQuery中,使用$(location).attr('href', url)
:
$(document).ready(function(){
var url = "https://www.youtube.com/watch?v=JwMKRevYa_M";
$(location).attr('href', url); // Using this
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
在原始 JavaScript中,有许多方法可以实现:
window.location.href="https://www.youtube.com/watch?v=JwMKRevYa_M";
Run Code Online (Sandbox Code Playgroud)
- 显式设置href属性.
window.location = "http://www.GameOfThrones.com";
Run Code Online (Sandbox Code Playgroud)
- 隐式执行因为window.location返回一个对象,默认情况下会设置其.href属性.
window.location.replace("http://www.stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
- 用新的窗口替换当前窗口的位置.
self.location = "http://www.somewebsite.com";
Run Code Online (Sandbox Code Playgroud)
- 设置当前窗口本身的位置.
以下是一段时间(3秒)后JavaScript重定向的示例:
<script>
setTimeout(function() {
window.location.href = "https://www.youtube.com/";
}, 3000);
</script>
Run Code Online (Sandbox Code Playgroud)
小智 27
在JavaScript和jQuery中,我们使用以下代码重定向页面:
window.location.href="http://google.com";
window.location.replace("page1.html");
Run Code Online (Sandbox Code Playgroud)
但是你可以在jQuery中创建一个函数来重定向页面:
jQuery.fn.redirect=function(url)
{
window.location.href=url;
}
Run Code Online (Sandbox Code Playgroud)
并调用此函数:
jQuery(window).redirect("http://stackoverflow.com/")
Run Code Online (Sandbox Code Playgroud)
小智 24
使用jQuery函数:
$.extend({
redirectPost: function(location, args) {
var form = '';
$.each(args, function(key, value) {
form += '<input type="hidden" name="' + key + '" value="' + value + '">';
});
$('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
}
});
Run Code Online (Sandbox Code Playgroud)
在您的代码中,您可以像这样使用它:
$.redirectPost("addPhotos.php", {pimreference: $("#pimreference").val(), tag: $("#tag").val()});
Run Code Online (Sandbox Code Playgroud)
vip*_*iya 19
只需在JavaScript中,您就可以使用以下内容重定向到特定页面:
window.location.replace("http://www.test.com");
Run Code Online (Sandbox Code Playgroud)
要么
location.replace("http://www.test.com");
Run Code Online (Sandbox Code Playgroud)
要么
window.location.href = "http://www.test.com";
Run Code Online (Sandbox Code Playgroud)
使用jQuery:
$(window).attr("location","http://www.test.com");
Run Code Online (Sandbox Code Playgroud)
Jay*_*dav 17
<script type="text/javascript">
var url = "https://yourdomain.com";
// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
{
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
// All other browsers
else { window.location.replace(url); }
</script>
Run Code Online (Sandbox Code Playgroud)
Amj*_*d K 17
Javascript非常广泛.如果您想跳转到另一个页面,您有三个选项.
window.location.href='otherpage.com';
window.location.assign('otherpage.com');
//and...
window.location.replace('otherpage.com');
Run Code Online (Sandbox Code Playgroud)
如果你想要移动到其他页面,你可以使用任何一个,如果这是你的要求但是所有这三个选项仅限于不同的情况.根据你的要求明智地选择.
如果你对这个概念的更多知识感兴趣.你可以进一步了解.
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
Run Code Online (Sandbox Code Playgroud)
Vin*_*rma 16
您可以像下面的代码一样使用它,其中getRequestToForwardPage
是请求映射(URL).您也可以使用您的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";
}
});
Run Code Online (Sandbox Code Playgroud)
这适用于相同的应用程序环境.
如果您只想使用jquery特定代码,那么以下代码可能有所帮助:
$(location).attr('href',"http://www.google.com");
$jq(window).attr("location","http://www.google.com");
$(location).prop('href',"http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
小智 15
以下是重定向到其他页面的代码,超时为10秒.
<script>
function Redirect()
{
window.location="http://www.adarshkr.com";
}
document.write("You will be redirected to a new page in 10 seconds.");
setTimeout('Redirect()', 10000);
</script>
Run Code Online (Sandbox Code Playgroud)
你也可以这样做,点击按钮使用location.assign:
<input type="button" value="Load new document" onclick="newPage()">
<script>
function newPage() {
window.location.assign("http://www.adarshkr.com")
}
</script>
Run Code Online (Sandbox Code Playgroud)
mad*_*hur 14
您可以使用:
window.location.replace("http://www.example.com/");
Run Code Online (Sandbox Code Playgroud)
该replace()
方法不会将原始页面保存在会话历史记录中,因此用户无法使用后退按钮返回并再次重定向.注意:在这种情况下,浏览器后退按钮将被停用.
但是,如果您想要效果与点击链接一样,您应该去:
window.location.href = "http://www.example.com/";
Run Code Online (Sandbox Code Playgroud)
在这种情况下,浏览器后退按钮将处于活动状态.
Abh*_*yay 14
这很容易实现.您可以使用:
window.location.href = "http://www.example.com/";
Run Code Online (Sandbox Code Playgroud)
这将记住上一页的历史记录.因此,可以通过单击浏览器的后退按钮返回.
要么:
window.location.replace("http://www.example.com/");
Run Code Online (Sandbox Code Playgroud)
此方法不记得上一页的历史记录.在这种情况下,后退按钮变为禁用状态.
cas*_*yle 14
使用:
function redirect(a) {
location = a
}
Run Code Online (Sandbox Code Playgroud)
并称之为: redirect([url]);
没有必要为href
后location
,因为它是隐含的.
小智 14
<script type="text/javascript">
if(window.location.href === "http://stackoverflow.com") {
window.location.replace("https://www.google.co.in/");
}
</script>
Run Code Online (Sandbox Code Playgroud)
San*_*shK 13
您可以使用以下方法重定向页面:
通过在头部使用元标记 - <meta http-equiv="refresh" content="0;url=http://your-page-url.com" />
.请注意,content="0;
...用于在您需要重定向页面的秒数之后
使用JavaScript: window.location.href = "http://your-page-url.com";
通过使用jQuery: $(location).attr('href', 'http://yourPage.com/');
HD.*_*D.. 13
单页应用程序,在同一应用程序路径中
window.location.pathname = '/stack';
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
location.href = "http://stack.com";
window.location = "http://stack.com";
Run Code Online (Sandbox Code Playgroud)
jQuery的:
$(location).attr('href', "http://www.stack.com");
$(window).attr('location', "http://www.stack.com");
Run Code Online (Sandbox Code Playgroud)
Angular 4
import { Router } from '@angular/router';
export class NavtabComponent{
constructor(private router: Router) {
}
this.router.navigate(['bookings/taxi']);
}
Run Code Online (Sandbox Code Playgroud)
wil*_*der 13
您可以在脚本部分中为Button click事件编写Url.Action,如下所示.
function onclick() {
location.href = '@Url.Action("Index", "Home")';
}
Run Code Online (Sandbox Code Playgroud)
Man*_*ash 12
我只是添加另一种方式:
要将您网站的任何特定页面/链接重定向到另一个页面,只需添加以下代码行:
<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>
Run Code Online (Sandbox Code Playgroud)
对于现实生活中的例子,
<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>
Run Code Online (Sandbox Code Playgroud)
通过使用此简单代码,您可以重定向整个站点或任何单个页面.
Ale*_*ton 12
如果您更喜欢使用纯JavaScript,我意识到在Firefox 中使用document.location.href = "https://example.com"
或window.location.href = "https://example.com"
导致兼容性问题.而是尝试使用:
location.href = "https://example.com";
location.replace("http://example.com");
Run Code Online (Sandbox Code Playgroud)
在我的情况下已经解决了这个问题.祝好运!
use*_*506 11
我已经使用了JavaScript的函数redirect().它正在发挥作用.
<script type="text/javascript">
$(function () {
//It's similar to HTTP redirect
window.location.replace("http://www.Technomark.in");
//It's similar to clicking on a link
window.location.href = "Http://www.Technomark.in";
})
</script>
Run Code Online (Sandbox Code Playgroud)
Moh*_*med 11
location.assign():
通过将路径传递到路径路径来分配路径路径.即使在分配路径后,Assign也会为您提供历史记录.
使用方法:应该将值传递给它.
例如: location.assign("http://google.com")
location.href
可以定义给它一个路径...一旦它设置它将重定向到一个给定的路径,它将保持历史...
使用方法:应将值赋值给它.
例如: location.href = "http://google.com"
location.replace():
如果您不想保留历史记录,将有助于替换路径.一旦你更换它的路径它就不会给你一个历史记录.
使用方法:值应传入其中.
例如: location.replace("http://google.com")
assign()
并且href
相似,都可以保存历史.assign
将通过传递值和href工作分配.
您可以使用JavaScript本身实现它,而无需通过分配使用jQuery,
window.location = "http://google.com"
location.href = "http://google.com"
Run Code Online (Sandbox Code Playgroud)
您可以使用下面的jQuery实现类似的功能.它会像上面一样完全相同,
$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
你可以很容易地理解两者之间的区别......
这是Location对象,
And*_*rut 11
使用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);
}
Run Code Online (Sandbox Code Playgroud)
RuN*_*ruN 10
如果您想简单地重定向到同一个应用程序中的路由
window.location.pathname = '/examplepath'
Run Code Online (Sandbox Code Playgroud)
将是要走的路.
从客户端进行重定向的所有方法:
<!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>
Run Code Online (Sandbox Code Playgroud)
使用jQuery/JavaScript重定向用户
通过在jQuery或JavaScript中使用location对象,我们可以将用户重定向到另一个网页.
在jQuery中
将用户从一个页面重定向到另一个页面的代码是:
var url = 'http://www.example.com';
$(location).attr('href', url);
Run Code Online (Sandbox Code Playgroud)
在JavaScript中
将用户从一个页面重定向到另一个页面的代码是:
var url = 'http://www.example.com';
window.location.href = url;
Run Code Online (Sandbox Code Playgroud)
要么
var url = 'http://www.example.com';
window.location = url;
Run Code Online (Sandbox Code Playgroud)
这就是我使用它的方式.
window.location.replace('yourPage.aspx');
// If you're on root and redirection page is also on the root
window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');
// If you're in sub directory and redirection page is also in some other sub directory.
Run Code Online (Sandbox Code Playgroud)
以我的工作经验来看,JavaScript重定向要好得多。
这取决于您要如何更改位置。如果要在用户历史记录中记录网站,请使用window.location.href='ur website';
。否则,无需登录历史记录即可使用window.location.replace("your website");
。
归档时间: |
|
查看次数: |
5960837 次 |
最近记录: |