jQuery cookie,设置上次访问的页面并重定向到上次访问的新会话 [英] jQuery cookies, set last visited page and redirect to last visited on new session

查看:264
本文介绍了jQuery cookie,设置上次访问的页面并重定向到上次访问的新会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



客户端设置最后访问的Cookie链接到外部页面时的页面。



我使用的是 https://github.com/carhartl/jquery-cookie 插件,方便您整合Cookie。



我的代码到目前为止:

  //获取当前URL 
var complete_url = document .URL;

//定义最后一个位置cookie
var last_location = $ .cookie('last_location');

//页读取集last_location作为当前位置
$(document).ready(function(){
$ .cookie('last_location',complete_url,{
expires:0,
path:'/'
});
});

//如果page_location设置导航到该位置
if(last_location&& last_location!= complete_url){
window.location = last_location;
}

这显然不工作,因为你会陷入无限循环因为这是我从圣诞节回来的第一天。



如果你能够帮助那将是伟大的。 p>

如果这个不清楚,我也很抱歉,如果需要,我可以尝试并重新输入。



谢谢。 / p>

[NOT SOLVED]



  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;
}
}

[NEW SOLUTION]

我提出了一个不依赖于document.referrer的新解决方案

  var last_location = $ .cookie('last_location'),
current_location = document.URL;

//初始检查cookie是否设置并且不等于当前位置
if(last_location& last_location!== current_location){
window.location = last_location;
}

//设置模式
var pattern = new RegExp(window.location.host);

//在任何链接上单击测试如果href没有与模式
$('a')匹配click(function(){
var href = $(this).attr('href');
if(pattern.test(href)!== true){
//如果没有找到匹配,链接是外部的, cookie
$ .cookie('last_location',document.URL,{
expires:1,
path:'/'
});
} else {
//如果链接是内部删除cookie并继续正常
$ .removeCookie('last_location',{
path:'/'
});
}
});


解决方案

我将在用户关联时设置Cookie到外部页面,而不是每次访问您客户的网站的页面。
所以我把一个事件监听器放到这个链接。

  $('。your_external_links')。on ',function(evt){
$ .cookie('last_location',document.URL,{
expires:7,//请阅读[NOTE_1]
path:'/'
}
}

[NOTE_1]



阅读Cookie插件的文档,过期部分说


到期



。Value可以是一个数字,它将被解释为从创建时间开始的日期或一个Date对象,如果省略,cookie将成为会话cookie
所以如果你希望该cookie永不过期, 。
例如:expires:9999


[END NOTE_1]



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

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

//检查用户是否来自外部域
if(comesFromUrl.indexOf(mySiteDomain)=== -1){
var last_location = $ .cookie('last_location '),
current_location = document.URL;

//检查cookie是否存在并且其值不是当前位置
if(typeof last_location!==undefined
& last_location!== current_location ){
//这里可以选择如果删除cookie或刷新它。随你便。
$ .removeCookie('last_location');
window.location = last_location;
}
}


So I am building an app for a client in jQuery, this is placed in an IFrame on their website. This app reads data from a JSON that is generated by their CMS and then sets page layouts etc from a query string.

The client me to set a cookie of the last visited page when linked off to an external page. Then on coming back to the app it will check if a cookie has been set, if so then redirect to that last visited page.

I am using https://github.com/carhartl/jquery-cookie plugin for easier cookie integration.

My code so far:

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

This obviously doesn't work as you'd get stuck in an infinite loop and I am struggling to get this right as it is my first day back at work from Christmas, ha.

If you are able to help that would be great!

Also I'm sorry if this is unclear I can try and reword it if need be.

Thanks.

[NOT SOLVED]

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;
    }
}

[NEW SOLUTION]
I have come up with a new solution that doesn't depend on the 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: '/'
        });
    }
});

解决方案

I would set the cookie when the user is linked off to an external page and not everytime he visit a page of your client's website. So I put an event listener to this links.

$('.your_external_links').on('click', function(evt){
    $.cookie('last_location', document.URL, {
        expires: 7, // Please read the [NOTE_1]
        path: '/'
    }
}

[NOTE_1]

Reading the documentation of the cookie plugin, 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"

[END NOTE_1]

Then when the user comes back I'll check if he comes from an outside link. If so I'll check also the cookie and redirect the user to the last location visited.

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;
    }
}

这篇关于jQuery cookie,设置上次访问的页面并重定向到上次访问的新会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆