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


当前回答

  1. 指定位置( ) :

    将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。

    用法:数值应传递到它中 。

    例如: location.assign("http://google.com")

    Enter image description here

  2. 位置.href

    定义可以给它一个路径... 并且一旦它建立,它会重新定位到一个指定路径, 它会保存历史...

    用法:值应该被指定到它中 。

    例如: location.href = "http://google.com"

  3. 位置. replace () :

    如果您不想保留历史, 它会帮助您替换一条路径。 一旦您替换了一条路径, 它不会给您一个历史 。

    用法:数值应传递到它中。

    例如: location.replace("http://google.com")

    Enter image description here


assign()href两者相似,可以保留历史。assign通过分配,通过传递一个价值和粗略的作品来工作。

您可以使用 JavaScript 自己实现它, 而不用 jQuery 来指定 :

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

使用下面的jQuery 也可以实现类似的目标。 它会做与上面完全相同的,

$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");

你可以很容易地理解 两者之间的区别...

这里是定位对象,

Location API from chrome

其他回答

  1. 指定位置( ) :

    将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。

    用法:数值应传递到它中 。

    例如: location.assign("http://google.com")

    Enter image description here

  2. 位置.href

    定义可以给它一个路径... 并且一旦它建立,它会重新定位到一个指定路径, 它会保存历史...

    用法:值应该被指定到它中 。

    例如: location.href = "http://google.com"

  3. 位置. replace () :

    如果您不想保留历史, 它会帮助您替换一条路径。 一旦您替换了一条路径, 它不会给您一个历史 。

    用法:数值应传递到它中。

    例如: location.replace("http://google.com")

    Enter image description here


assign()href两者相似,可以保留历史。assign通过分配,通过传递一个价值和粗略的作品来工作。

您可以使用 JavaScript 自己实现它, 而不用 jQuery 来指定 :

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

使用下面的jQuery 也可以实现类似的目标。 它会做与上面完全相同的,

$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");

你可以很容易地理解 两者之间的区别...

这里是定位对象,

Location API from chrome

我们不能简单地使用 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";

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

标注描述 :

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 为您提供了许多获取和更改当前在浏览器地址栏中显示的 URL 的方法。 所有这些方法都使用位置对象, 这是窗口对象的属性。 您可以创建一个新的位置对象, 其当前 URL 如下 。

var currentLocation = window.location;

URL 基本结构

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

enter image description here

  1. 协议 -- 指定协议名称用于访问互联网上的资源。 (HTTP(没有 SSL)或HTPS(没有 SSL))

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

  3. 端口 -- -- 用于识别因特网或其他网络电文到达服务器时要发送到的具体过程的端口号。

  4. 路径 - 路径为网络客户端想要访问的主机内的具体资源提供信息。 例如, stackoverflow. com/ index.html 。

  5. 查询 -- -- 查询字符串跟随路径组件,并提供一个资源可用于某种目的的信息字符串(例如,作为搜索的参数或作为要处理的数据)。

  6. hash - URL 的锁定部分, 包括 hash 符号 (#) 。

使用这些位置对象属性,您可以访问所有这些 URL 组件

  1. 散数- 发送或返回 URL 的锁定部分。
  2. 主机主机- 发送或返回 URL 主机名和端口。
  3. 主机名- 发送或返回 URL 主机名。
  4. 小时( href)- 设置或返回整个 URL 。
  5. 路径名- set 或返回 URL 的路径名。
  6. 端口端口- set 或返回服务器用于 URL 的端口号。
  7. 议定书议定书- 发送或返回 URL 的协议。
  8. 搜索搜索- set 或返回 URL 的查询部分

如果您现在想要更改页面或将用户重定向到其他页面, 您可以使用该页面href像这样的位置对象的属性

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

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

位置对象还有这三种方法

  1. (指定)- 装入新文档。
  2. 重新装入()- 重新装入当前文档。
  3. (替换)-- -- 将当前文档替换为新文档

您可以使用指派 () 并替换将重定向到其它页面的方法

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

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

转让 () 和替换 () 如何不同- 替换() 方法与指定() 方法() 之间的差别是,替换() 从文档历史中删除当前文档的 URL, 意思是无法使用“ 回溯” 按钮返回原始文档。所以,如果您想要装入新文档,则使用指定() 方法, 并想要给此选项返回原始文档 。

您可以使用jj 查询像这样

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

这样你就可以把用户 转到其他的URL上。