jQuery cookies,设置上次访问的页面并重定向到上次访问的新会话

jQuery cookies, set last visited page and redirect to last visited on new session

所以我正在用 jQuery 为客户构建一个应用程序,它被放置在他们网站上的一个 IFrame 中。此应用程序从其 CMS 生成的 JSON 中读取数据,然后从查询字符串中设置页面布局等。

当链接到外部页面时,客户端我设置了最后访问页面的 cookie。然后返回到应用程序时,它将检查是否已设置 cookie,如果已设置,则重定向到上次访问的页面。

我正在使用 https://github.com/carhartl/jquery-cookie 插件来简化 cookie 集成。

到目前为止我的代码:

//Get current URL
var complete_url = document.URL;

//Define last location cookie
var last_location = $.cookie('last_location');

//On page read set last_location as current location
$(document).ready(function() {
    $.cookie('last_location', complete_url, {
        expires: 0,
        path: '/'
    });
});

//if page_location is set navigate to that location
if (last_location && last_location != complete_url) {
    window.location = last_location;
}

这显然是行不通的,因为你会陷入无限循环,我正在努力解决这个问题,因为这是我从圣诞节回来工作的第一天,哈。

如果你能提供帮助那就太好了!

此外,如果不清楚,我很抱歉,如果需要,我可以尝试改写它。

谢谢。

[未解决]

var pattern = new RegExp(window.location.host);

$('a').click(function() {
    var href = $(this).attr('href');
    if(pattern.test(href) !== true) {
        $.cookie('last_location', document.URL, {
            expires: 7,
            path: '/'
        });
    }
});

var referrer = window.parent.document.referrer,
    current = document.domain;

if(referrer.indexOf(current) === -1) {
    var last_location = $.cookie('last_location'),
        current_location = document.URL;

    if(typeof last_location !== 'undefined' && last_location !== current_location) {
        $.removeCookie('last_location');
        window.location = last_location;
    }
}

[新解决方案]
我想出了一个不依赖于 document.referrer

的新解决方案
var last_location = $.cookie('last_location'),
    current_location = document.URL;

//Initial check if cookie is set and not equal to current location
if (last_location && last_location !== current_location) {
    window.location = last_location;
}

//Set pattern
var pattern = new RegExp(window.location.host);

//On any link click test if the href does not have a match with the pattern
$('a').click(function() {
    var href = $(this).attr('href');
    if (pattern.test(href) !== true) {
        //If no match found, the link is external and adding current url to the cookies
        $.cookie('last_location', document.URL, {
            expires: 1,
            path: '/'
        });
    } else {
        //Else if link is internal remove cookie and continue as normal
        $.removeCookie('last_location', {
            path: '/'
        });
    }
});

我会在用户 link 转到外部页面时设置 cookie,而不是每次他访问您客户网站的页面时。 所以我给这个 links.

设置了一个事件监听器
$('.your_external_links').on('click', function(evt){
    $.cookie('last_location', document.URL, {
        expires: 7, // Please read the [NOTE_1]
        path: '/'
    }
}

[NOTE_1]

正在阅读 cookie 插件的文档,the expire section says:

expires

Define lifetime of the cookie. Value can be a Number which will be interpreted as days from time of creation or a Date object. If omitted, the cookie becomes a session cookie. So if you want that cookie never expire put a big number inside of this. For example: "expires: 9999"

[结束NOTE_1]

然后当用户回来时我会检查他是否来自外部link。如果是这样,我还会检查 cookie 并将用户重定向到上次访问的位置。

var comesFromUrl  = document.referrer,
    mySiteDomain = document.domain;

// Check if user comes from an external domain
if(comesFromUrl.indexOf(mySiteDomain) === -1) {
    var last_location    = $.cookie('last_location'),
        current_location = document.URL;

    // Check if cookie exists and if its value is not the current location
    if(typeof last_location !== "undefined"
       && last_location !== current_location) {
        // Here is possible to choose if remove the cookie or refresh it. It's up to you.
        $.removeCookie('last_location');
        window.location = last_location;
    }
}