重定向到新页面但记住原始位置

Bra*_*roy 3 javascript php cookies jquery redirect

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

if (condition) {
        window.location = "http://mywebsite.com/landingpage"
}
Run Code Online (Sandbox Code Playgroud)

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

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

我在想,也许是这样的:

// 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'));
Run Code Online (Sandbox Code Playgroud)

Ani*_*ini 5

您可以在重定向之前获取页面的 urldocument.referrer

var referrer =  document.referrer;
window.location = referrer;
Run Code Online (Sandbox Code Playgroud)

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