【问题标题】:Remember selection - then redirect to correct home page on visits记住选择 - 然后在访问时重定向到正确的主页
【发布时间】:2016-11-10 18:20:34
【问题描述】:

我们的网站有 2 个子域 (http://financial.walkerdendle.co.uk & http://technical.walkerdendle.co.uk),但我们有一个登录页面 (www.walkerdendle.co.uk)

我希望在登陆页面上保持不变,但使用 Cookie 实现某种 JavaScript,这样当用户第一次选择网站(财务或技术)时,他们会被重定向,但如果他们访问登陆页面页面 (www.walkerdendle.co.uk) 稍后,他们会自动重定向到他们上次选择的选择(财务或技术)。

【问题讨论】:

  • 听起来像是一个计划!你试过这样做吗?
  • 如果用户想在未来某个日期返回着陆页怎么办?
  • 你能澄清你的问题吗?很难回答一个陈述;-)
  • 只需添加一些有关您将使用的技术的信息,否则没人知道您在寻找什么。

标签: javascript cookies selection


【解决方案1】:

下面的演示“登陆页面”完全符合 OP 的要求。当用户点击一个链接时,URL 首先被保存到一个 cookie 中。要求用户确认此操作。当用户将来访问该页面时,他们会自动重定向到他们之前的选择。

根据我的经验,用户更喜欢为页面添加书签,而不是通过这种方式重定向。然而,我不能决定 OP 的要求或什么对他/她的公司最有效。

<html>
<head>
<script type="text/javascript">

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

})()

</script>  
</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>

【讨论】:

  • 这看起来不错,当我设计它时,链接似乎没有去任何地方
猜你喜欢
  • 2013-12-12
  • 2013-03-04
  • 2019-03-16
  • 2019-10-27
  • 1970-01-01
  • 2016-04-01
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多