【问题标题】:Redirect to new page but remember original location重定向到新页面但记住原始位置
【发布时间】:2013-12-05 21:46:18
【问题描述】:

我想在某些条件下将用户引导到“登陆”页面,例如

if (condition) {
        window.location = "http://mywebsite.com/landingpage"
}

但我还想记住用户导航到的原始目的地,以便我可以在着陆页上放置一个重定向链接,将用户移动到原始目的地。

我知道如何使用 PHP 来做到这一点,但我完全需要 JS/jQuery 来实现这一点。我可以使用 cookie,如果这能让事情变得更容易的话。

我在想,也许是这样的:

// Condition when user moves to the page
if (condition) {
    // Set cookie with value of current page
    $.cookie('locational_cookie', window.location, { expires: 1}); 
    // Redirect
    window.location = "http://mywebsite.com/landingpage";
}

// When on the landing page, change the href of the "back" link to the original URL that is in the cookie.
$(".landingpage a.back").attr("href", $.cookie('locational_cookie'));

【问题讨论】:

  • 一种方法是将当前位置作为 GET 发送

标签: javascript php jquery redirect cookies


【解决方案1】:

您可以使用document.referrer在重定向之前获取页面的url

var referrer =  document.referrer;
window.location = referrer;

此链接将重定向到初始页面。

【讨论】:

    【解决方案2】:

    试试这个:

    var pathname = window.location.pathname;
    window.location = "http://mywebsite.com/landingpage?url="+pathname;
    

    【讨论】:

      【解决方案3】:

      为了安全起见,我会将其发送到查询字符串中并对其进行编码。

      if(condition) {
          var loc=encodeURIComponent(document.location.href);
          window.location = "http://mywebsite.com/landingpage?loc=" + loc;
          }
      

      【讨论】:

        【解决方案4】:

        在 PHP 中:

        header('Location: http://mywebsite.com/landingpage?redirect_uri=' .
            urlencode($_SERVER['REQUEST_URI']));
        exit();
        

        编辑: 哎呀,你想要 JavaScript。谢谢@user2648239!读得有点太快了。其他人已经用 JavaScript 回答了。

        【讨论】:

        • Asker 明确提到他知道用 PHP 来做。他需要 Javascript/JQuery 代码。
        【解决方案5】:

        这是我解决它的方法。它的作用是:如果有人阻止了广告,则重定向到一个页面,解释为什么广告对您的网站很重要,然后允许用户导航回他们的原始目的地。由于设置了 cookie,页面只会显示一次。

        // When on the landing page, change the href of the "back" link to the original URL that is in the cookie.
        if ($("body").hasClass("page-id-7876")) { // Class of the landing page
            // Set link
            $("a.cookie-link").attr("href", $.cookie('locational_cookie'));
            // Remove locational cookie
            $.removeCookie('locational_cookie', {path: '/'});
            $.cookie('ads_checked', 'true', { expires: 365, path: '/' });
        }
        
        if($("#secondary ins.adsbygoogle").is(':empty') || $("#secondary ins.adsbygoogle").height() === 0 || !$("#secondary ins.adsbygoogle").is(":visible")) {
            $("#secondary ins.adsbygoogle").html('New HTML');
        
            if ($.cookie('locational_cookie') == null && $.cookie('ads_checked') == null) {
                // Set cookie with value of current page
                $.cookie('locational_cookie', window.location, { expires: 7, path: '/' });
                // Redirect
                window.location = "http://www.mysite.com/pagetoredirectto";
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-21
          • 2011-06-29
          相关资源
          最近更新 更多