所以我正在为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: '/'
});
}
});
答案 0 :(得分:1)
我会在用户链接到外部页面时设置cookie,而不是每次访问客户网站的页面时都设置cookie。 所以我把一个事件监听器放到这个链接上。
$('.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:
的到期强>
定义cookie的生命周期。值可以是一个数字,它将被解释为创建时的天数或Date对象。如果省略,cookie将成为会话cookie。 因此,如果您希望cookie永远不会过期,请将此内容放入其中。 例如:“expires:9999”
[END NOTE_1]
然后当用户回来时,我会检查他是否来自外部链接。如果是这样,我还会检查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;
}
}