使用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

其他回答

我只是补充另一种方式:

要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:

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

以真实生活为例

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

通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。

与 jQuery 合作 :

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

简在JavaScript 贾斯克里普特,您可以通过下列方式将方向转向特定页面:

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

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

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

使用 j 查询 :

$(window).attr("location","http://www.test.com");

单页应用在同一申请路线内

window.location.pathname = '/stack';

JavaScript :

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

jj 查询 :

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

角形 4

import { Router } from '@angular/router';
export class NavtabComponent{
    constructor(private router: Router) {
    }
    this.router.navigate(['bookings/taxi']);
}

这是重定向到其他页面的代码 超过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>

您也可以这样操作, 点击按钮时使用位置. 指派 :

<input type="button" value="Load new document" onclick="newPage()">
<script>
    function newPage() {
        window.location.assign("http://www.adarshkr.com")
    }
</script>