简体   繁体   English

HTML页面上重新加载不同的HTML

[英]Html page on reload different html

第一次加载html并将其显示为“是”,第二次加载或从缓存加载时应显示其他一些“否”。

As you're using jQuery, it seemed easiest to use the jQuery Cookie plugin : 当您使用jQuery时,似乎最容易使用jQuery Cookie插件

// assigns the value returned by the cookie to the variable,
// if no cookie, or no value, is returned, the string is assigned instead.
var cookieValue = $.cookie('firstVisit') || 'Yes';

// if there's no cookie, then a cookie is set.
if (!$.cookie('firstVisit')) {
    $.cookie('firstVisit', 'No', {
        expires: 7 // expires in 7 days, adjust to taste.
    });
}

$('#firstVisit').text(cookieValue);​

JS Fiddle demo . JS小提琴演示

The above code updated to be a little tidier, and less repetitive: 上面的代码已更新得更整洁,并且不再重复:

// if the cookie is not set, the cookieVal variable is falsey
var cookieVal = $.cookie('firstVisit'),
    // creating a reference to the relevant div
    div = $('#firstVisit');

// if the cookieVal evaluates to false the cookie is set,
// and the text of the div is 'Yes'
if (!cookieVal) {
    $.cookie('firstVisit', 'No');
    div.text('Yes');
}
// otherwise...
else {
    // text of the div is equal to the value returned from the cookie
    div.text(cookieVal);
}​

JS Fiddle demo . JS小提琴演示

You can use cookies to recognize a returning visitor: 您可以使用Cookie识别回访者:

<html>
<body>

<script language="javascript">

// sets a cookie with a certain name, value and optional expiration in days
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;
}

// retrieves the value of a cookie of a certain name (or returns undefined)
function getCookie(c_name) {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++) {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name) return unescape(y);
    }
}

// if the cookie was set, then the user has been here already
if (getCookie("visited") == "true") {
    document.write("NO");
} else {
    // otherwise, set the cookie and assume the user has not been here before
    setCookie("visited", "true");
    document.write("YES");  
}

</script>

</body>
</html>

See: http://en.wikipedia.org/wiki/HTTP_cookie 请参阅: http//en.wikipedia.org/wiki/HTTP_cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM