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


当前回答

以下是一个时间延迟重定向。 您可以将延迟时间设定为任何您想要的时间 :

<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Your Document Title</title>
    <script type="text/javascript">
        function delayer(delay) {
            onLoad = setTimeout('window.location.href = "http://www.google.com/"', delay);
        }
    </script>
</head>

<body>
    <script>
        delayer(8000)
    </script>
    <div>You will be redirected in 8 seconds!</div>
</body>

</html>

其他回答

使用 JavaScript :

方法1:

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

方法2:

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

使用 j 查询 :

方法1:美元(地点)

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

方法2:可再使用功能

jQuery.fn.redirectTo = function(url){
    window.location.href = url;
}

jQuery(window).redirectTo("http://google.com");

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

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

就这么简单!

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

但如果有人想重回主页 他可能会使用下面的片段

window.location = window.location.host

如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。

您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台

使用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);
}

使用 jQuery 函数 :

$.extend({
  redirectPost: function(location, args) {
    var form = '';
    $.each(args, function(key, value) {
      form += '<input type="hidden" name="' + key + '" value="' + value + '">';
    });
    $('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
  }
});

在你的代码中,你使用它这样:

$.redirectPost("addPhotos.php", {pimreference:  $("#pimreference").val(), tag: $("#tag").val()});