最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redirect to new page but remember original location - Stack Overflow

programmeradmin2浏览0评论

I want to send users to a "landing" page on some conditions, e.g.

if (condition) {
        window.location = ""
}

but I also want to remember the original destination that a user was navigating to so that I can place a re-redirect link on the landing page which moves the user to the original destination.

I know how to do this with PHP, but I exclusively need JS/jQuery for this. I can use cookies, if that makes things easier.

I was thinking, maybe something like this:

// 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 = "";
}

// 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'));

I want to send users to a "landing" page on some conditions, e.g.

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

but I also want to remember the original destination that a user was navigating to so that I can place a re-redirect link on the landing page which moves the user to the original destination.

I know how to do this with PHP, but I exclusively need JS/jQuery for this. I can use cookies, if that makes things easier.

I was thinking, maybe something like this:

// 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./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'));
Share Improve this question edited Nov 21, 2013 at 12:25 Mark Simpson 2,3842 gold badges23 silver badges32 bronze badges asked Nov 21, 2013 at 12:11 Bram VanroyBram Vanroy 28.7k27 gold badges147 silver badges265 bronze badges 1
  • 1 one method is to send the current location as GET – Zword Commented Nov 21, 2013 at 12:13
Add a ment  | 

5 Answers 5

Reset to default 5

You can get the url of the page before redirect using document.referrer

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

This link will redirect to the initial page.

Try this:

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

I would send it on the query string and would also encode it just to be on the safe side.

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

Here is how I solved it. What it does is: if some one has ads blocked, redirect to a page explaining why ads are important for your website and then allowing the user to navigate back to their original destination. The page will only be showed once because of the cookies that have been set.

// 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./pagetoredirectto";
    }
}

In PHP:

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

EDIT: Whoops, you wanted JavaScript. Thanks @user2648239! Read that one a little too quickly. Others have already answered with JavaScript.

发布评论

评论列表(0)

  1. 暂无评论