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


当前回答

您可以在脚本部分的按钮点击事件下写入 Url. 动作 。

function onclick() {
    location.href = '@Url.Action("Index", "Home")';
}

其他回答

ECMAScript 6 + jQuery,85 字节

$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")

请不要杀我,这是个玩笑,这是个玩笑这是一个笑话。

这“为问题提供了答案”, 意思是它要求找到“使用jQuery”的解决方案,

Ferrybig显然需要解释笑话(还是开玩笑,

其他的答案是使用 jQuery 的attr()会 议 日 程 和 议 程locationwindow不必要的物体。

这个答案也滥用了它, 但以一种更荒谬的方式。 它不是用它来设定位置, 而是用它来设置位置,attr()获取设置位置的函数。

函数被命名jQueryCode即使它没有任何jQuery, 并且号召一个函数somethingCode特别是当事情甚至不是语言的时候。

“85字节”是指高尔夫代码。高尔夫显然不是你在高尔夫代码之外应该做的,此外,这个答案显然不是实际的高尔夫。

基本上, 畏缩。

标准“ vanilla” JavaScript 重定向页面的方法

window.location.href = 'newPage.html';

或更简单: (自window全球)

location.href = 'newPage.html';

如果你在这里,因为你损失HTTP_REFERERER 重定向时, 继续读取 :

(否则忽略此最后部分)


下一节针对的是那些使用HTTP_REFERER作为许多安全措施之一的安全措施(尽管这不是一个伟大的保护措施)。因特网探索者 8使用任何形式的 JavaScript 页面重定向( 位置.href 等) 时, 这些变量会丢失 。

我们将在下面实施一个替代方案:IE8 更低这样我们就不会失去 HTTP_REFERER。 否则,你几乎总是可以简单地使用 HTTP_REFERERER。window.location.href.

测试HTTP_REFERER(URL 粘贴、 会话等)能够帮助判断请求是否合法 。(注:正如Droop在评论中指出的,也有一些办法可以对这些裁判员进行变通/补充)


简单的交叉浏览器测试解决方案( 返回窗口. place.href 用于 Internet Explorer 9+ 和所有其他浏览器)

用法 :redirect('anotherpage.aspx');

function redirect (url) {
    var ua        = navigator.userAgent.toLowerCase(),
        isIE      = ua.indexOf('msie') !== -1,
        version   = parseInt(ua.substr(4, 2), 10);

    // Internet Explorer 8 and lower
    if (isIE && version < 9) {
        var link = document.createElement('a');
        link.href = url;
        document.body.appendChild(link);
        link.click();
    }

    // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
    else { 
        window.location.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";

从客户的侧面调整方向:

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

这很容易执行。 您可以使用 :

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

这将记住上一页的历史。 这样可以点击浏览器的后键返回历史 。

或者:

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

此方法不记得上一页的历史。 在此情况下, 后端按钮会被禁用 。