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


当前回答

这样做有许多方法。

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

其他回答

您可以在 jQuery 中像这样重定向 :

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

您可以在没有jQuery 的情况下这样做 :

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

如果你只想要jQuery, 你可以做它喜欢:

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

我只是补充另一种方式:

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

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

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

jj 查询,使用$(location).attr('href', url):

$(document).ready(function(){
    var url = "https://www.youtube.com/watch?v=JwMKRevYa_M";
    $(location).attr('href', url); // Using this
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

原始 JavaScript 贾斯克里普特实现这一点有若干途径:

window.location.href="https://www.youtube.com/watch?v=JwMKRevYa_M";

- 明确设定 href 属性。

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

- 是否暗含它 因为窗口。 位置返回一个对象, 默认设置它的. href 属性 。

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

- 将当前窗口的位置替换为新窗口。

self.location = "http://www.somewebsite.com";

- 设置当前窗口本身的位置。

以下是JavaScript在一定时间(3秒)后重新定向的例子:

<script>
    setTimeout(function() {
        window.location.href = "https://www.youtube.com/";
    }, 3000);
</script>

所以,问题在于如何改变页面方向, 而不是如何改变网页方向?

您只需要为此使用 JavaScript 即可。 这里有一些小代码, 可以创建一个动态重定向页面 。

<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

如果你去那个链接 它会自动引导你到堆叠流. com.

链接到文档

这就是你如何制造简单使用 JavaScript 重定向页面

编辑 :

还有一件事需要指出。window.location.replace在我的代码中,因为我认为它适合 重置页面, 但是,你必须知道,当使用window.location.replace当您在浏览器中按回按钮时,它会被重定向回到重定向页面, 它会回到之前的页面, 看看这个小演示的东西。

示例:

进程:家居存储店 => 将页面重定向到谷歌 => 谷格

当谷歌:谷格 => 在浏览器中的后退按钮 => 家居存储店

因此,如果这适合您的需求, 那么一切都应该很好。 如果您想要在浏览器历史中包含重置页面, 替换此

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

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