Code for the product features recently browsed

Source: Internet
Author: User
Tags set cookie

The product has been browsed recently to implement code functions. The client cookie is used for instances. we store the browsed product information to the client cookie, then, use js to read whether the cookie file exists. If so, call it and assign a value to lastep. In this way, the product has been browsed recently.

The product has been browsed recently to implement code functions. The client cookie is used for instances. we store the browsed product information to the client cookie, then, use js to read whether the cookie file exists. If so, call it and assign a value to lastep. In this way, the product has been browsed recently.

Is the Page code,
<Div id = "latestp"> </div>
<Script type = "text/javascript tutorial">
Var historyp;
// ID of the latest accessed item
Var nid = "New Product ID ";
// Set the number of browsing records saved by the cookie
Var N = 10;
Var count = 0;
// Determine whether a cookie exists
If ($. cookie ('smile1314h') = null) // cookie does not exist
{
// Create a new cookie to save browsing records
$. Cookie ('smile1314h', nid, {expires: 7, path :'/'});
// $. Cookie ('smile1314h', nid, {expires: 7, path: '/', domain: 'smile1314. com', secure: true });
}
Else // cookies already exist
{
// Obtain the ID of the browsed item
Historyp = $. cookie ('smile1314h ');
Var check_result = $ ('# latestp ');
Check_result.html (' ');
// Ajax obtains the information list based on the product number
$. Ajax ({// an Ajax Process
Type: "get ",
Url: "/Comm/getLatestProduct. ashx ",
DataType: 'html ',
Data: "P_Id =" + historyp,
Success: function (json ){
Check_result.html ("");
Check_result.html (json );
}});
// Break down strings into Arrays
Var pArray = historyp. split (',');
// The latest product number is placed at the beginning
Historyp = nid;
// Determine whether the product ID exists in the most recent access record
For (var I = 0; I <pArray. length; I ++)
{
If (pArray [I]! = Nid)
{
Historyp = historyp + "," + pArray [I];
Count ++;
If (count = N-1)
{
Break;
}
}
}
// Modify the cookie value
$. Cookie ('smile1314h', historyp );
}
</Script>

The following is the code of the cookie. js file.
/* JCookie. js */
JQuery. cookie = function (name, value, options ){
If (typeof value! = 'Undefined') {// name and value given, set cookie
Options = options | {};
If (value = null ){
Value = '';
Options. expires =-1;
}
Var expires = '';
If (options. expires & (typeof options. expires = 'number' | options. expires. toUTCString )){
Var date;
If (typeof options. expires = 'number '){
Date = new Date ();
Date. setTime (date. getTime () + (options. expires * 24x60*60*1000 ));
} Else {
Date = options. expires;
}
Expires = '; expires =' + date. toUTCString (); // use expires attribute, max-age is not supported by IE
}
Var path = options. path? '; Path =' + options. path :'';
Var domain = options. domain? '; Domain =' + options. domain :'';
Var secure = options. secure? '; Secure ':'';
Document. cookie = [name, '=', encodeURIComponent (value), expires, path, domain, secure]. join ('');
} Else {// only name given, get cookie
Var cookieValue = null;
If (document. cookie & document. cookie! = ''){
Var cookies = document. cookie. split (';');
For (var I = 0; I <cookies. length; I ++ ){
Var cookie = jQuery. trim (cookies [I]);
// Does this cookie string begin with the name we want?
If (cookie. substring (0, name. length + 1) = (name + '= ')){
CookieValue = decodeURIComponent (cookie. substring (name. length + 1 ));
Break;
}
}
}
Return cookieValue;
}
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.