选择页面上的Cookie作为将来访问的主页

时间:2018-08-16 07:54:45

标签: javascript cookies

我是编码方面的新手。当前有一个允许用户选择其首选语言页面作为其主页的项目。为此,我使用了用户她在堆栈溢出中发布的一些代码。但它似乎无法正常工作,卡在了循环加载中。有人可以指出我的问题吗?

var redirect = (function() {    

    var DEBUG = false,   // true to disable redirects
        NAME = 'redirect=', 
        DAYS = 7, // number of days to redirect
        cookie = document.cookie,
        expires;

    // read cookie and redirect 
    if (cookie.indexOf( NAME ) >= 0) {
        cookie = cookie.split( NAME ).pop().split(';').shift();
        cookie = decodeURIComponent( cookie );
        if (DEBUG) alert( 'Redirect\n' + cookie); else location.href = cookie;
    }

    // set redirect
    return function( url ) {
        if (confirm('Go to this link automatically on your next visit?')) {
            expires = new Date();
            expires.setDate( expires.getDate() + DAYS );
            cookie = 'redirect=' + encodeURIComponent( url ) + ';expires=' + expires.toUTCString() + ';path=/';   
            document.cookie = cookie;
        }
        if (!DEBUG) location.href = url;
    }

})()
<html>
<head>

</head>
<body>
    <img src="http://technical.walkerdendle.co.uk/_resx/images/logo.png">
    <br>
    <a href="javascript:void(0)" onclick="redirect('http://technical.walkerdendle.co.uk')">Technical</a>    
    <br>
    <a href="javascript:void(0)" onclick="redirect('http://financial.walkerdendle.co.uk')">Financial</a>
</body>
</html>

0 个答案:

没有答案