如何redirect到另一个网页?

如何使用jQuery或纯JavaScript将用户从一个页面redirect到另一个页面?

一个不会简单地使用jQueryredirect

jQuery不是必需的, window.location.replace(...)将最好地模拟HTTPredirect。

window.location.replace(...)比使用window.location.href要好,因为replace()不会将原始页面保留在会话历史logging中,这意味着用户不会陷入永无止境的后退,button惨败。

如果你想模拟某人点击链接,请使用location.href

如果您想模拟HTTPredirect,请使用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"; 

警告:这个答案只是提供了一个可能的解决scheme; 这显然不是最好的解决scheme,因为它需要jQuery。 相反,更喜欢纯粹的JavaScript解决scheme。

 $(location).attr('href', 'http://stackoverflow.com') 

标准的“香草”JavaScriptredirect页面的方式:

window.location.href = 'newPage.html';


如果因为在redirect时丢失 HTTP_REFERER而来到这里,请继续阅读:


以下部分针对那些使用HTTP_REFERER作为许多安全措施之一(尽pipe这不是一个很好的保护措施)。 如果您使用的是Internet Explorer 8或更低版本,则在使用任何forms的JavaScript页面redirect(location.href等)时,这些variables会丢失。

下面我们将实现一个替代IE8&更低,这样我们不会失去HTTP_REFERER。 否则,你几乎总是可以简单地使用window.location.href

使用HTTP_REFERER (URL粘贴,会话等) 进行testing可以有助于判断请求是否合法。 注意:也有办法绕过/欺骗这些推荐人,正如评论中的下垂链接所指出的那样)


简单的跨浏览器testing解决scheme(适用于Internet Explorer 9+和所有其他浏览器的回退到window.location.href)

用法: redirect('anotherpage.aspx');

 function redirect (url) { var ua = navigator.userAgent.toLowerCase(), isIE = ua.indexOf('msie') !== -1, version = parseInt(ua.substr(4, 2), 10); // Internet Explorer 8 and lower if (isIE && version < 9) { var link = document.createElement('a'); link.href = url; document.body.appendChild(link); link.click(); } // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does) else { window.location.href = url; } } 

使用:

 // 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') 

这适用于每个浏览器:

 window.location.href = 'your_url'; 

如果你对你正在做的事情有更多的描述,这将有所帮助。 如果您正在尝试生成分页数据,则可以通过一些选项来实现。 您可以为每个想要直接获取的页面生成单独的链接。

 <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> ... 

请注意,示例中的当前页面在代码和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 }); }); 

我也认为location.replace(URL)是最好的方法,但是如果你想通知search引擎你的redirect(他们不分析JavaScript代码来看redirect),你应该添加rel="canonical"元标记到您的网站。

添加一个带有HTML刷新元标记的无脚本部分也是一个很好的解决scheme。 我build议你使用这个JavaScriptredirect工具来创buildredirect。 它还具有Internet Explorer支持来传递HTTP引用。

示例代码没有延迟看起来像这样:

 <!-- Pleace 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 --> 

但是如果有人想redirect回主页,那么他可能会使用下面的代码片段。

 window.location = window.location.host 

如果你有三个不同的环境,如开发,分期和生产,这将是有帮助的。

您只需将这些文字放在Chrome控制台或Firebug的控制台中即可浏览此窗口或window.location对象。

JavaScript提供了许多方法来检索和更改浏览器地址栏中显示的当前URL。 所有这些方法都使用Location对象,它是Window对象的一个​​属性。 您可以创build一个具有当前URL的新的Location对象,如下所示。

 var currentLocation = window.location; 

URL的基本结构

 <protocol>//<hostname>:<port>/<pathname><search><hash> 

在这里输入图像描述

  1. 协议 – 指定用于访问Internet上资源的协议名称。 (HTTP(不带SSL)或HTTPS(带SSL))

  2. 主机名 – 主机名指定拥有资源的主机。 例如,www.stackoverflow.com。 服务器使用主机的名称提供服务。

  3. 端口 – 端口号,用于识别Internet或其他networking消息到达服务器时要转发到的特定进程。

  4. path名 – 该path提供有关Web客户端要访问的主机内的特定资源的信息。 例如,stackoverflow.com/index.html。

  5. 查询 – 查询string跟随path组件,并提供资源可用于某种目的(例如,作为search参数或要处理的数据)的一串信息。

  6. 散列 – URL的锚点部分包含散列符号(#)。

通过这些Location对象属性,您可以访问所有这些URL组件

  1. 散列值 – 设置或返回URL的锚点部分。
  2. 主机 – 设置或返回URL的主机名和端口。
  3. hostname – 设置或返回URL的主机名。
  4. href – 设置或返回整个url。
  5. path名 – 设置或返回URL的path名称。
  6. 端口 – 设置或返回服务器用于URL的端口号。
  7. 协议 – 设置或返回URL的协议。
  8. search – 设置或返回URL的查询部分

现在,如果您想更改页面或将用户redirect到其他页面,则可以使用Location对象的href属性,如下所示

您可以使用Location对象的href属性。

 window.location.href = "http://www.stackoverflow.com"; 

位置对象也有这三个方法

  1. assign() – 加载一个新文档。
  2. reload() – 重新加载当前文档。
  3. replace() – 用新的replace当前文档

您可以使用assign()并replace方法来redirect到其他页面

 location.assign("http://www.stackoverflow.com"); location.replace("http://www.stackoverflow.com"); 

如何分配()和replace()不同 – replace()方法和assign()方法()之间的区别是,replace()从文档历史logging中删除当前文档的URL,意味着它是不可能使用“返回”button导航回到原始文档。 所以,如果你想加载一个新的文档,那么使用assign()方法,并且要给出选项导航回原始文档。

你也可以像这样使用jQuery来更改位置对象的href属性

 $(location).attr('href',url); 

因此,您可以将用户redirect到其他url。

应该能够使用window.location进行设置。

例:

window.location = "http://www.stackoverflow.com";

这里是关于这个问题的过去的post:

如何在JavaScript / jQuery中redirect到另一个网页?

在我开始之前,jQuery是一个用于DOM操作的JavaScript库。 所以你不应该使用jQuery来进行页面redirect。

来自Jquery.com的引用:

虽然jQuery可能在较老的浏览器版本中运行时没有大问题,但是我们并不主动testingjQuery,通常也不会修正可能出现在其中的错误。

它被发现在这里: https : //jquery.com/browser-support/

所以jQuery不是一个全面的,全部的向后兼容的解决scheme。

以下使用原始JavaScript的解决scheme适用于所有浏览器,并且已经很长时间达到标准,所以您不需要任何库来支持跨浏览器。

3000毫秒后,此页面将redirect到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> 

不同的选项如下:

 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" 

当使用replace时,后退button不会回到redirect页面,就好像它从未在历史中一样。 如果您希望用户能够返回redirect页面,请使用window.location.hrefwindow.location.assign 。 如果您确实使用允许用户返回redirect页面的选项,请记住,当您进入redirect页面时,它会将您redirect回。 所以selectredirect选项时要考虑到这一点。 在页面只有在用户完成操作后redirect的情况下,后退button历史中的页面才可以。 但是,如果页面自动redirect,那么您应该使用replace,以便用户可以使用后退button,而不会强制回到redirect发送的页面。

您也可以使用元数据运行页面redirect,如下所示。

META刷新

 <meta http-equiv="refresh" content="0;url=http://evil.com/" /> 

META位置

 <meta http-equiv="location" content="URL=http://evil.com" /> 

基地劫持

 <base href="http://evil.com/" /> 

可以在这个页面find更多的方法来redirect你不知情的客户端到他们不希望去的页面(其中没有一个依赖于jQuery):

https://code.google.com/p/html5security/wiki/RedirectionMethods

我也想指出,人们不喜欢随机redirect。 只有在绝对需要时才会redirect人员。 如果你开始随机redirect,他们将永远不会再去你的网站。

下一部分是假设的:

您也可能会被报告为恶意网站。 如果发生这种情况,那么当用户点击您网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。 如果人们在您的网站上报告不好的体验,search引擎可能会开始降低您的评分。

请查看Google网站pipe理员指南有关redirect的信息: 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> 

如果将两个页面示例组合在一起,您将会有一个重新路由的婴儿循环,这将保证您的用户永远不会想再次使用您的网站。

 var url = 'asdf.html'; window.location.href = url; 

你可以做到这一点,没有jQuery:

 window.location = "http://yourdomain.com"; 

如果你只想要jQuery,那么你可以这样做:

 $jq(window).attr("location","http://yourdomain.com"); 

这适用于jQuery:

 $(window).attr("location", "http://google.fr"); 

#HTML页面redirect使用jQuery / JavaScript

试试这个例子代码:

 function YourJavaScriptFunction() { var i = $('#login').val(); if (i == 'login') window.location = "login.php"; else window.location = "Logout.php"; } 

如果你想给一个完整的URL为window.location = "www.google.co.in";

所以,问题是如何做一个redirect页面,而不是如何redirect到一个网站?

你只需要使用JavaScript来做到这一点。 以下是一些可以创builddynamicredirect页面的小代码。

 <script> var url = window.location.search.split('url=')[1]; // Get the URL after ?url= if( url ) window.location.replace(url); </script> 

所以说,你只需把这个片段放到你网站上的redirect/index.html文件中就可以像这样使用它。

http://www.mywebsite.com/redirect?url=http://stackoverflow.com

如果你去那个链接,它会自动将你redirect到stackoverflow.com

链接到文档

这就是你如何用JavaScript创build一个简单的redirect页面

编辑:

还有一件事要注意。 我已经在我的代码中添加了window.location.replace ,因为我认为它适合redirect页面,但是,您必须知道,当使用window.location.replace并且redirect时,当您按下浏览器中的后退button时,将不会回到redirect页面,它会回到之前的页面,看看这个小演示的东西。

例:

这个过程: store home => redirect页面到google => google

当在谷歌: 谷歌 => 后退button浏览器 => 存储在家中

所以,如果这符合你的需求,那么一切都应该是好的。 如果您想在浏览器历史logging中包含redirect页面,请将其replace

 if( url ) window.location.replace(url); 

 if( url ) window.location.href = url; 

在您的点击function,只需添加:

 window.location.href = "The URL where you want to redirect"; $('#id').click(function(){ window.location.href = "http://www.google.com"; }); 

尝试这个:

 location.assign("http://www.google.com"); 

示例的代码片段 。

jQuery是不需要的。 你可以这样做:

 window.open("URL","_self","","") 

这很容易!

发起HTTP请求的最好方法是使用document.loacation.href.replace('URL')

你需要把这行代码放在你的代码中

 $(location).attr('href',"http://stackoverflow.com"); 

如果你没有jquery,那就用javascript去吧

 window.location.replace("http://stackoverflow.com"); 

基本上jQuery是一个JavaScript库,为了做一些像redirect这样的事情,你可以使用纯JavaScript,所以在这种情况下,你有3个select使用香草JavaScript:

1)使用位置replace ,这将取代当前的页面历史logging,意味着不可能使用后退button返回到原始页面。

 window.location.replace("http://stackoverflow.com"); 

2)使用位置分配 ,这将保持你的历史和使用后退button,你可以回到原来的页面:

 window.location.assign("http://stackoverflow.com"); 

3)我推荐使用以前的方法之一,但这可能是使用纯JavaScript的第三种select:

 window.location.href="http://stackoverflow.com"; 

你也可以在jQuery中编写一个函数来处理它,但不build议这样做,因为它只有一行纯JavaScript函数,如果你已经在窗口范围内,你也可以使用上面所有的函数而不需要窗口,例如window.location.replace("http://stackoverflow.com"); 可以location.replace("http://stackoverflow.com");

另外我在下面的图片上展示他们:

location.replace

先写好。 您希望在应用程序中导航,以便从您的应用程序的另一个链接进行另一个链接 这里是代码:

 window.location.href = "http://www.google.com"; 

如果你想浏览应用程序中的页面,那么我也有代码,如果你想。

你可以像这样在jQuery中redirect:

 $(location).attr('href', 'http://yourPage.com/'); 

只是redirect到一个页面:

  window.location.href = "/destination.html"; 

或者,如果您需要延迟:

 setTimeout(function () { window.location.href = "/destination.html"; }, 2000); // Time in milliseconds 

jQuery允许您轻松地从网页中select元素。 您可以在页面中find任何您想要的内容,然后使用jQuery添加特殊效果,对用户操作做出反应,或者在所选元素内部或外部显示和隐藏内容。 所有这些任务从知道如何select一个元素开始 。

 function redirect () { $('selection').animate({ marginLeft: 100px; //Put some CSS animation here }, 500, function () { // OK, finished jQuery staff, let's go redirect window.location.href = "/destination.html"; }) 

想象一下,有人写了一个脚本/插件是10000行的代码? 那么,使用jQuery,你可以用一两行连接到这个代码。

在JavaScript和jQuery中,我们可以使用下面的代码将一个页面redirect到另一个页面:

 window.location.href="http://google.com"; window.location.replace("page1.html"); 

ECMAScript 6 + jQuery,85个字节

 $({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com") 

请不要杀我,这是一个笑话。 这是一个笑话。 这是个玩笑。

这样做“提供了一个问题的答案”,因为它提出了一个“使用jQuery”的解决scheme,在这种情况下需要以某种方式强迫它进入方程式。

Ferrybig显然需要这个笑话解释(仍然是在开玩笑,我敢肯定评论表上的选项是有限的),所以不要着急:

其他答案是使用jQuery的attr()不必要的locationwindow对象。

这个答案也滥用了,但是以一种更为荒谬的方式。 而不是使用它来设置位置,它使用attr()来检索一个设置位置的函数。

该函数被命名为jQueryCode ,尽pipe没有任何关于它的jQuery,并且调用一个函数somethingCode是非常糟糕的,特别是当某些东西甚至不是语言时。

“85字节”是Code Golf的参考。 高尔夫运动显然不是高尔夫之外你应该做的事情,而且这个答案显然不是高尔夫运动。

基本上,畏缩。

这是一个时间延迟redirect。 你可以设置延迟时间为任何你想要的:

 <!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> 

我只是用另一个更新的jQuery方法来更新这个荒谬:

 var url = 'http://www.fiftywaystoleaveyourlocation.com'; $(location).prop('href', url); 

使用Javascript:

 window.location.href='www.your_url.com'; window.top.location.href='www.your_url.com'; window.location.replace('www.your_url.com'); 

jQuery的:

 var url='www.your_url.com'; $(location).attr('href',url); $(location).prop('href',url);//instead of location you can use window 

有三种主要的方法来做到这一点,

 window.location.href='blaah.com'; window.location.assign('blaah.com'); 

和…

 window.location.replace('blaah.com'); 

对于传统的redirect,最后一个是最好的,因为它不会保存您在search历史中redirect之前所到达的页面。 但是,如果您只想使用JavaScript打开选项卡,则可以使用上述任何一种。 1

编辑: window前缀是可选的。