2012-07-20 62 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