我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?


当前回答

使用 JavaScript:

方法1:

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

方法2:

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

使用 jQuery:

方法1: $(位置)

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

方法2:可重新使用的功能

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

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

其他回答

您可以使用它如下代码,在那里 getRequestToForwardPage 是请求地图(URL)。

function savePopUp(){
    $.blockUI();
    $.ajax({
        url:"GuestHouseProcessor?roomType="+$("#roomType").val(),
        data: $("#popForm").serialize(),
        dataType: "json",
        error: (function() {
            alert("Server Error");
            $.unblockUI();
    }),
    success: function(map) {
        $("#layer1").hide();
        $.unblockUI();
        window.location = "getRequestToForwardPage";
    }
});

这就是申请的相同背景。

如果您只想使用 jQuery 特定的代码,那么以下代码可以帮助:

 $(location).attr('href',"http://www.google.com");
 $jq(window).attr("location","http://www.google.com");
 $(location).prop('href',"http://www.google.com"); 

我也认为 location.replace(URL)是最好的方式,但如果你想通知搜索引擎你的重新引导(他们不分析JavaScript代码查看重新引导),你应该添加 rel="canonical" meta 标签到你的网站。

添加一个Noscript 部分,其中有一个 HTML 更新 meta 标签,也是一个很好的解决方案. 我建议您使用这个 JavaScript 重定向工具来创建重定向。

样品代码无延迟看起来如下:

<!-- Place this snippet right after opening the head tag to make it work properly -->

<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->

<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://yourdomain.example/"/>
<noscript>
    <meta http-equiv="refresh" content="0;URL=https://yourdomain.example/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://yourdomain.example/";
    if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
    {
        document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }
    else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->

您可以使用 jQuery 如下:

window.location = "http://yourdomain.com";

如果你只想要jQuery,那么你可以这样做:

$jq(window).attr("location","http://yourdomain.com");

原始问题:“如何使用 jQuery 重新引导?”,因此答案实施 jQuery >> 补充使用案例。


要重定向到使用JavaScript的页面:

window.location.href = "/contact/";

如果您需要延迟:

setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds

jQuery 允许您轻松地从 Web 页面中选择元素. 您可以在页面上找到您想要的任何东西,然后使用 jQuery 添加特殊效果,对用户行动作出反应,或显示和隐藏您选择的元素内部或外部的内容。

$('a,img').on('click',function(e){
  e.preventDefault();
  $(this).animate({
    opacity: 0 //Put some CSS animation here
  }, 500);
  setTimeout(function(){
    // OK, finished jQuery staff, let's go redirect
    window.location.href = "/contact/";
  },500);
});

想象一下,有人写了一张10万行代码的脚本/插件,使用jQuery,你可以用一行或两行连接到这个代码。

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

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