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


当前回答

我已经使用 JavaScript 的函数重定向() 。 它正在工作 。

<script type="text/javascript">
    $(function () {
        //It's similar to HTTP redirect
        window.location.replace("http://www.Technomark.in");

        //It's similar to clicking on a link
        window.location.href = "Http://www.Technomark.in";
    })
</script>

其他回答

警告: 警告:答案只是作为一种可能的解决办法而已;显然最好的解决方案,因为它需要jQuery。 相反,它更喜欢纯 JavaScript 解决方案。

$(location).prop('href', 'http://stackoverflow.com')

每个浏览器都使用此功能 :

window.location.href = 'your_url';

您可以使用以下方法重定向页面:

  1. 通过在头部使用元标记 -<meta http-equiv="refresh" content="0;url=http://your-page-url.com" />注意:content="0;... 用于在您需要多少秒后重定向页面

  2. 使用 JavaScript :window.location.href = "http://your-page-url.com";

  3. 使用 jQuery 查询 :$(location).attr('href', 'http://yourPage.com/');

使用location.replace()这将调整您的方向, 但不保存上一页的历史。 提交表格时最好使用此选项 。

但当你想保留自己的历史时,你必须使用location.href=//path.

实例:

// Form with steps
document.getElementById('#next').onclick = function() {
   window.location.href='/step2' // Iteration of steps;
}

// Go to next step
document.getElementById('#back').onclick = function() {
   window.history.back();
}

// Finish
document.getElementById('#finish').onclick = function() {
   window.location.href = '/success';
}

// On success page
window.onload = function() {
    setTimeout(function() {
       window.location.replace('/home'); // I can't go back to success page by pressing the back button
    },3000);
}

从客户的侧面调整方向:

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