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


当前回答

从客户的侧面调整方向:

<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript and jQuery example to redirect a page or URL </title>
    </head>
    <body>
        <div id="redirect">
            <h2>Redirecting to another page</h2>
        </div>

        <script src="scripts/jquery-1.6.2.min.js"></script>
        <script>
            // JavaScript code to redirect a URL
            window.location.replace("http://stackoverflow.com");
            // window.location.replace('http://code.shouttoday.com');

            // Another way to redirect page using JavaScript

            // window.location.assign('http://code.shouttoday.com');
            // window.location.href = 'http://code.shouttoday.com';
            // document.location.href = '/relativePath';

            //jQuery code to redirect a page or URL
            $(document).ready(function(){
                //var url = "http://code.shouttoday.com";
                //$(location).attr('href',url);
                // $(window).attr('location',url)
                //$(location).prop('href', url)
            });
        </script>
    </body>
</html>

其他回答

使用 :

function redirect(a) {
    location = a
}

并称之为:redirect([url]);

没必要href之后location,因为它是隐含的。

jj 查询不需要。 您可以做到这一点 :

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

就这么简单!

提出HTTP要求的最佳方法是:document.loacation.href.replace('URL').

使用 jQuery/JavaScript 重定向用户

使用 jQuery 或 JavaScript 中的定位对象, 我们可以将用户重定向到另一个网页 。

在 jQuery 中

将用户从一页转到另一页的代码是:

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

在 JavaScript 中

将用户从一页转到另一页的代码是:

var url = 'http://www.example.com';
window.location.href = url;

var url = 'http://www.example.com';
window.location = url;

这样做有三个主要途径,

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

还有...

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

最后一个是最好的, 用于传统的重定向, 因为它不会保存您在搜索历史中被重定向之前的页面。 但是, 如果您只想用 JavaScript 打开一个标签, 您可以使用上述任何选项 。1

爱迪特:window前缀是可选的 。

首先正确写入。 您想要在应用程序中浏览另一个链接, 从应用程序中浏览另一个链接。 以下是代码 :

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

如果您想要浏览您应用程序中的页面, 我也有代码, 如果您想要的话 。