2012-07-20 70 views
0

第一次加载html,它显示文本为是,第二次当从缓存中重新加载或加载它应该显示一些其他文本编号。Html页面上重新加载不同的html

+0

好的,你需要问一个具体的问题,这样我们就知道我们正在回答什么(以及你的期望)。另外,如果你能证明你已经尝试过某些东西,你是否遇到了一个特殊的问题,这会有所帮助? – 2012-07-20 21:10:17

+2

@ karpra-你能否在这里澄清一下问题?我假设你的意思是:“_是否有一种方法可以在第一次加载页面时在文本中显示'是',但是当页面再次加载或从缓存加载时显示'否'?” – s0d4pop 2012-07-20 21:11:13

+0

设置cookie,尝试jquery cookie – 2012-07-20 21:16:52

回答

0

您可以使用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> 

参见:http://en.wikipedia.org/wiki/HTTP_cookie

+0

这个需求是我第一次在提交html1之后得到一个html/jsp的html1,它在iOS移动设备上安装了一个配置文件,并且在完成安装后,当你点击它时,它会打开一个浏览器这次我上次访问过的相同的html1,我希望它重定向到一个不同的URL。目前我找到相同的html1页面,我不知道如何重新定向它新的URL当它回来.. – karpra 2012-07-20 21:36:47

+0

感谢您的这种做法,但我想控制代码不是基于cookie,但会议上我有一个代码如果我错了,请更正代码片段.. out.println(“”我想检查一个脚本的条件让标签,并显示基于会话的HTML .. – karpra 2012-07-22 01:54:56

+0

这里是我试过out.println代码片段( \t \t \t \t \t \t \t “” + \t \t \t \t \t \t \t \t \t“在JSP表单中使用Post方法。 “+ \t \t \t \t \t \t \t \t \t” “+ \t \t \t \t \t \t \t \t \t \t ”<形式行动= \“/ mportal /注册\” 方法= \ “交\”> “+ \t \t \t \t \t \t \t \t \t \t <%if(session。的getAttribute( “测试”)== NULL){ \t \t \t \t \t \t \t \t \t \t “” + \t \t \t \t \t \t \t \t \t \t \t \t “其他” + \t \t \t \t \t \t \t \t \t \t \t \t “Click Me ”+ \t \t \t \t \t \t \t \t \t \t \t “}%>” + \t \t \t \t \t \t \t \t \t“” + \t \t \t \t \t \t \t \t \t \t \t \t \t “ ”+ \t \t \t \t \t \t \t \t \t“”); – karpra 2012-07-22 01:56:54

1

当你使用jQuery,它似乎最容易使用的jQuery Cookie plugin

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

上面的代码更新为一个小整洁,和更少的重复:

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

+0

+1 yep,就像那样 – 2012-07-20 22:16:44