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


当前回答

简在JavaScript 贾斯克里普特,您可以通过下列方式将方向转向特定页面:

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

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

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

使用 j 查询 :

$(window).attr("location","http://www.test.com");

其他回答

在 JavaScript 和 jQuery 中, 我们可以使用以下代码将一页改到另一页 :

window.location.href="http://google.com";
window.location.replace("page1.html");

应该能够设定使用window.location.

示例:

window.location = "https://stackoverflow.com/";

以下是关于此议题的过去文章:我该如何转到另一个网页?

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

在 JavaScript 和 jQuery 中, 我们使用以下代码重定向页面 :

window.location.href="http://google.com";
window.location.replace("page1.html");

但您可以在 jQuery 中设定函数, 以重定向页面 :

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

调用此函数 :

jQuery(window).redirect("http://stackoverflow.com/")

标注描述 :

window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');

杰克里:

var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window