0

我想让我的 html 页面在我第一次进入页面时出现一个欢迎对话框。我不希望每次加载页面时都出现对话框。我只知道 html、css 和 javascript。是否可以停止每次出现此对话框?

 <body  onload="welcome()"> 
    <h1>Hello World</h1>    
    <script>
            function welcome(){
    alert(" Welcome to Music.com");
        }
    </script>
    </body>

每次我重新加载页面时,欢迎框都会再次出现。我该如何阻止它?

4

5 回答 5

3

好吧,这似乎是一个问题,因为 JS 只能将数据存储在不会永远存在的 Sessions/Cookies 中,但是 - 我能想到一种方法(Cookies 除外)。

您可以使用localStorage来保存用户已经看到的对话框。

或者您可以开始学习node.js - 它的 Javascript,但用于服务器端编程。

于 2013-06-27T20:35:11.943 回答
2

过去,我使用过jQuery cookie 插件。以下是代码外观的示例:

//check for cookie - https://github.com/carhartl/jquery-cookie
if ($.cookie('FirstTime') !== 'close') {

    //show promo pop-up

    $('.promoCloseButton').click(function() {

      //hide promo

      //set cookie on close
      $.cookie('FirstTime', 'close', {
        path : '/',
        expires : 120
      });

    });

  }
于 2013-06-27T20:38:18.907 回答
1

演示:http: //jsfiddle.net/abc123/JYsdN/1/

编辑:添加评论

JS:

    //Gets the cookie with given c_name taken from w3school
function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    } else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}

//Sets the cookie with given c_name to given value for exdays taken from w3school
function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

//Function for GUID creation
function s4() {
  return Math.floor((1 + Math.random()) * 0x10000)
             .toString(16)
             .substring(1);
};

//Function for GUID creation
function guid() {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
         s4() + '-' + s4() + s4() + s4();
}

//Checks if the user has the cookie
function checkCookie() {
    //attempts to get the cookie
    var user = getCookie("user");
    //If the user doesn't have a cookie
    if (user == null || user == "") {
        //Welcome them
        alert("Welcome!!!");
        //Create a GUID
        user = guid();
        //Set the user's cookie to their GUID expiring in 365 days
        if (user != null && user != "") {
            setCookie("user", user, 365);
        }
    }
}

//on document.ready call checkCookie
checkCookie();
于 2013-06-27T20:39:52.273 回答
0

您可以制作一个持续到您页面上的用户会话结束的 cookie。这样,如果他们刷新页面,他们的会话就不会结束,并且该框也不会出现。这是一个简单的 cookie 教程的链接http://www.w3schools.com/js/js_cookies.asp

于 2013-06-27T20:30:18.250 回答
0

只需使用 html 和 javascript,您就需要使用 cookie。

如果尚未访问该站点,请设置 cookie 并显示 div。否则什么也不做。

http://jsfiddle.net/Qm58q/

var cookie = "myCookies";
    if(!getCookie(cookie)){
        document.getElementById('welcome').className = "show";
        setCookie(cookie,"true",1)
    }


showWelcome();
于 2013-06-27T20:36:43.713 回答