【问题标题】:How to only show first time visitors specific tooltips?如何仅显示首次访问者特定的工具提示?
【发布时间】:2013-01-25 05:06:00
【问题描述】:

我只想让初次使用的用户“浏览”我的网站。我想用 JavaScript 做。我的意思是,如果用户从未看过我的网站并且这是他/她的第一次访问,他们将获得有关如何使用该网站的教程。如果他们再次访问或重新加载页面,他们不应该看到工具提示。这可以用 JavaScript Cookies 完成吗?我找到了一个 PHP 代码,但我现在想避免使用 PHP。

<?php
// Top of the page, before sending out ANY output to the page.
    $user_is_first_timer = !isset( $_COOKIE["FirstTimer"] );

// Set the cookie so that the message doesn't show again
    setcookie( "FirstTimer", 1, strtotime( '+1 year' ) );
?>




<H1>hi!</h1><br>


<!-- Put this anywhere on your page. -->
<?php if( $user_is_first_timer ): ?>
    Hello there! you're a first time user!.
<?php endif; ?>

如果用JS做不到,可以用.htaccess做吗?我还发现了这个:

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]

【问题讨论】:

  • 如果 cookie 不存在,请执行您的工具提示内容。否则,不要。
  • 我对 Cookie 没有任何经验,我在网上找不到与我的主题相关的任何内容。

标签: javascript html .htaccess cookies


【解决方案1】:

当然,尽管我更喜欢使用localStorage 来存储“用户之前访问过”信息,因为如果您设置了一个 cookie,那么该 cookie 将随每个发送到服务器。单身的。要求。制成。至。您的。服务器。

那么,试试这个:

if( !window.localStorage) {
    // no localStorage (old browser) - either fall back to cookie method,
    // or just do nothing and skip the whole tour thing - if the user
    // can't be bothered to upgrade, why should you bother to accomodate them?
}
else {
    if( !window.localStorage.isReturningVisitor) {
        // do all the tour stuff here
        window.localStorage.isReturningVisitor = true;
    }
}

【讨论】:

  • 是的!本地存储!我比饼干更喜欢它。我不知道这是可能的。我现在就测试一下。
  • 如果你要遵循“什么都不做”的建议,我建议if(window.localStorage &amp;&amp; !window.localStorage.isReturningVisitor) { /* do tour */ }
  • 你能创建一个 JSFiddle 吗?
  • @DaKoder 为什么需要一个?如果我这样做了,那将只是我答案中代码的字面复制和粘贴...
  • @Kolink 我的意思是,就像使用该代码创建示例窗口一样。
猜你喜欢
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 2019-04-26
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多