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


当前回答

但如果有人想重回主页 他可能会使用下面的片段

window.location = window.location.host

如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。

您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台

其他回答

我也认为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 -->

我只是不得不更新这种荒谬的 另一种新的jQuery方法:

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

我们不能简单地使用 jQuery 重置

j/ 查询没有必要,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";

应该能够设定使用window.location.

示例:

window.location = "https://stackoverflow.com/";

以下是关于此议题的过去文章:我该如何转到另一个网页?

原始问题:“如何使用 jQuery 重新定向?” , 因此, 答案执行 jQuery 补充使用案例 。


重定向到 JavaScript 的页面 :

window.location.href = "/contact/";

或者,如果您需要延迟:

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

jjQuery 允许您从网页中轻松选择元素。 您可以在页面中找到任何您想要的内容, 然后使用 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);
});

想象一下有人写了一个脚本/ 插件, 包含 10000 行代码 。 如果有 jQuery , 您可以用一两行代码连接到这个代码 。