[英]changing the text when the page reloads
我做了一個基本表格,在您輸入內容並提交Cookie后,顯示“ welcome” +名稱
我添加了第二個Cookie,當您重新加載同一頁面時,上述文本消失,並顯示“歡迎回來” +名稱。
我是使用Cookie的新手,無法正常使用該功能。
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<input id="user" type="user" name="user">
<input id = "submit" type="submit" name="submit">
</form>
<h1 id ="greeting" cookie_name ='name'></h1>
<h1 id ="welcome" cookie_name='visited'></h1>
<script
src="http://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script type="text/javascript" src="script/js.cookie.js"></script>
<script type="text/javascript" src="script/script.js"></script>
</body>
</html>
--------------------------------------------------------------------------
<script>
$(document).ready(function(){
if(Cookies.get('name')===undefined){
$('form').submit(function(){
var name = $('input').val()
Cookies.set('name', name)
Cookies.set('visited', 1)
})
}else{
$('form').css('display', 'none')
$('#greeting').append('Welcome ' + Cookies.get('name') + '!')
}
})
if(Cookies.get('visited')===undefined){
$('#greeting').text('Welcome again!')
}else{
$('#greeting').text('Welcome back ' + Cookies.get('name') + '!')
}
<script>
我實際上會使用sessionStorage.setItem('name', name);
存儲項目並檢查是否設置了像這樣的項目if (sessionStorage.getItem("name"))
檢查此鏈接
如果必須使用Cookies,那么此代碼對我來說就可以了:
$(document).ready(function () {
if (Cookies.get('name') === undefined) {
Cookies.set('name', 'alexis')
Cookies.set('visited', 1)
} else {
console.log('Name is already set');
}
})
if (Cookies.get('visited') === undefined) {
console.log('welcome for the first time')
} else {
console.log('welcome ' + Cookies.get('name'))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.