javascript 每次访问显示一个 Div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13730042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:38:20  来源:igfitidea点击:

Showing a Div Once per visit

javascriptjquerycookies

提问by JCBiggar

On my website I have a slider to only show on the Homepage. My home page looks something like this:

在我的网站上,我有一个只显示在主页上的滑块。我的主页看起来像这样:

<body>
  <div class="wrapper">
     <div class="header"></div>
     <div class="slider"></div>
   <div class="maincontent"></div>
 </div>
</body>

My Pages are the same layout, except I only want the Slider div to show on the first page. So when the user first arrives to the website, they can see the slider, but if they go to a different page, the slider is gone.

我的页面是相同的布局,除了我只希望 Slider div 显示在第一页上。所以当用户第一次到达网站时,他们可以看到滑块,但如果他们转到不同的页面,滑块就不见了。

I was wondering if there was a way to do that with, jquery? Cookies? If anyone has any suggestions, please let me know how I would do this. Thanks!

我想知道是否有办法用 jquery 做到这一点?饼干?如果有人有任何建议,请让我知道我将如何做到这一点。谢谢!

回答by Frog

Yes, there is a way to do this. In fact, there are multiple ways. You can do it both using Javascript and a server-side scripting language like PHP. I recommend using a server-side scripting language, because there's no point in adding an element to a page if it isn't used, while it does take up a tiny amount of bandwidth. Anyways, an example using both methods follows:

是的,有一种方法可以做到这一点。其实方法有很多种。您可以使用 Javascript 和服务器端脚本语言(如 PHP)来完成。我建议使用服务器端脚本语言,因为如果不使用元素,则向页面添加元素是没有意义的,而它确实会占用很少的带宽。无论如何,使用这两种方法的示例如下:

Using PHP:

使用 PHP:

<body>
<div class="wrapper">
<div class="header"></div>
<?php

if (!isset($_COOKIE['visisted'])) {
    setcookie('visited', true, time() + 3600 * 24); // Save a cookie for 1 day
    echo '<div class="slider"></div>';
}

?>
<div class="maincontent"></div>
</div>
</body>

And using Javascript:

并使用 Javascript:

<head>
<style type="text/css">
div#slider {
    /* Hide the div */
    display: none;
}
</style>
</head>

<body>
<div class="wrapper">
<div class="header"></div>
<div class="slider"></div>
<div class="maincontent"></div>
</div>

<script type="text/javascript">
var cookie = document.cookie;
if (cookie.indexOf('visited=', 0) == -1) {
    var expiration = new Date();
    expiration.setDate(expiration.getDate()+1);
    document.cookie = 'visited=1;expires=' + expiration + ';path=/';

    var element = document.getElementById('slider');
    element.style.display = 'block';
}
</script>
</body>

By the way, I assume this isn't your whole HTML page? Because obviously you should add a doctype, tags, etc.

顺便说一句,我假设这不是您的整个 HTML 页面?因为显然您应该添加文档类型、标签等。

回答by bonbonez

Sure, you can use:

当然,您可以使用:

  1. Cookies, that can be accessed from js
  2. HTML5 WebStorage, which is a great place too, but works only on new browsers
  1. 可以从 js 访问的 Cookies
  2. HTML5 WebStorage,这也是个好地方,但只适用于新浏览器

回答by SMillerNL

If you aren't bound to browsers I'd recommend to use the HTML5 LocalStorage variable.

如果您没有绑定到浏览器,我建议您使用 HTML5 LocalStorage 变量。

http://www.w3schools.com/html/html5_webstorage.asp

http://www.w3schools.com/html/html5_webstorage.asp

However if you are supposed to be browser independent (support IE6 etc.) I would go with cookies or a database with visited IP's.

但是,如果您应该独立于浏览器(支持 IE6 等),我会使用 cookie 或具有访问过的 IP 的数据库。