織夢(mèng)的這兩個(gè)函數(shù)很重要。他直接簡(jiǎn)化了JS的COOKIE。讓他由復(fù)雜變的簡(jiǎn)單了。這兩個(gè)函數(shù)在AJAX里面的。
//讀寫cookie函數(shù)
function GetCookie(c_name)
{
if
(document.cookie.length > 0)
{
c_start = document.cookie.indexOf(c_name +
"=")
if (c_start != -1)
{
c_start =
c_start + c_name.length + 1;
c_end
= document.cookie.indexOf(";",c_start);
if (c_end
== -1)
{
c_end =
document.cookie.length;
}
return
(document.cookie.substring(c_start,c_end));
}
}
return null
}
function SetCookie(c_name,value,expiredays)
{
var exdate = new
Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name
+ "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" +
exdate.toGMTString()); //使設(shè)置的有效時(shí)間正確。增加toGMTString()
}
-->
增加一個(gè)函數(shù)。
如下:
function getCookies(ids,title,img){
var saveid =
GetCookie('id');
var savetitle =
GetCookie('products');
var saveimg = GetCookie('images');
if(saveid != null
&& savetitle != null
&& saveimg != null)
{
var saveids =
saveid.split(',');
var savetitles =
savetitle.split(',');
var saveimgs = saveimg.split(',');
var hasid = false;
saveid = '';
savetitle = '';
saveimg = '';
j = 1;
for(i=saveids.length-1;i>=0;i--)
{
if(saveids[i]==ids
&& hasid) continue;
else {
if(saveids[i]==ids
&& !hasid) hasid = true;
saveid += (saveid=='' ? saveids[i] :
','+saveids[i]);
savetitle += (savetitle=='' ?
savetitles[i] : ','+savetitles[i]);
saveimg += (saveimg=='' ? saveimgs[i] :
','+saveimgs[i]);
j++;
if(j==10 &&
hasid) break;
if(j==9 &&
!hasid) break;
}
}
if(hasid) { return false; }
else
saveid += ','+ids;
savetitle += ','+title;
saveimg += ','+img;
SetCookie('id',saveid,1);
SetCookie('products',savetitle,1);
SetCookie('images',saveimg,1);
}
else
{
SetCookie('id',ids,1); //這個(gè)是ID。用來做鏈接用的
SetCookie('products',title,1); //這個(gè)是標(biāo)題,用來顯示在頁(yè)面的
SetCookie('images',img,1); //這個(gè)是圖片
}
}
2.在頁(yè)面引用js.
<script language="javascript"
type="text/javascript"
src="{dede:global.cfg_cmsurl/}/include/js/dedeajax2.js"></script>
<script language="javascript"
type="text/javascript">
getCookies({dede:field.id/},'{dede:field.title/}','{dede:field.litpic/}')
function CheckLogin2(){
var taget_obj =
document.getElementByIdx_x_x('_userlogin');
myajax = new
DedeAjax(taget_obj,false,false,'','','');
myajax.SendGet2("{dede:global.cfg_cmspath/}/member/ajax_loginsta2.php");
DedeXHTTP = null;
}
</script>
調(diào)用歷史記錄的代碼:
<div class="Recommend
h-394">
<h1
class="rec-bg"><span
class="span-rt-3"><a
onclick="destroy_cookie()">清空</a></span>最近瀏覽過的課程</h1>
<ul
class="w-223 browse " id="viewHistory">
<script
type="text/javascript">
function destroy_cookie(){
SetCookie('id','',1);
SetCookie('products','',1);
SetCookie('images','',1);
document.getElementByIdx_x_x('viewHistory').innerHTML="";
window.alert('你已成功清除瀏覽歷史記錄!');
}
</script>
<script
type="text/javascript">
var saveid
= GetCookie('id');
var
savetitle = GetCookie('products');
var saveimg = GetCookie('images');
if(saveid
!= null && savetitle != null)
{
var saveids =
saveid.split(',');
var savetitles =
savetitle.split(',');
var saveimgs = saveimg.split(',');
var hasid = false;
saveid = '';
savetitle = '';
saveimg = '';
for(i=saveids.length-1;i>=0;i--)
{
document.write("<li>"
+"<div class='w-53'><a
href=\"/plus/view.php?aid="+saveids[i]+"\"
title=\""+savetitles[i]+"\"><img
src='"+saveimgs[i]+"' width='52'
height='51'/></a></div><p
class='w-53-rt'><a
href=\"/plus/view.php?aid="+saveids[i]+"\"
title=\""+savetitles[i]+"\">"+savetitles[i]+"</a></p></li>");
}
}
</script>
</ul>
</div>
來源:互聯(lián)網(wǎng)。