Ajax+cookie realize NetEase news top effect.

Source: Internet
Author: User
Tags mysql tutorial php tutorial set cookie setcookie

Ajax+cookie realize NetEase news top effect.
<a href= ' web effects: get_top_num (1); ' > Top </a>

Ajax

function GetCookie (c_name) {//Detect cookies
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 unescape (document.cookie.substring (c_start,c_end));
}
}
Return "";
}

function Setcookie (c_name,value,expiredays) {//Set cookie
var exdate=new Date ();
Exdate.setdate (Exdate.getdate () +expiredays);
document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)? "": "; expires=" +exdate.togmtstring ());
}

Function Createxmlhttprequest () { //Create XMLHttpRequest Object
 if (window. ActiveXObject) {  //ie
  try {
   return new ActiveXObject (" Microsoft.XMLHTTP ");
  } catch (e) {
   return
  }
 }else if window. XMLHttpRequest) { //mozilla,firefox
  try {
   return new XMLHttpRequest ();
&NBSP;&NBSP} catch (e) {
   return;
  }
 
}

Function Get_top_num (ID) { //keynote
 var get_top_cookie=getcookie (' get_top_ ' +id+ ' _cookie ');
 if (get_top_cookie!=null && get_top_cookie!= "") {
  alert (' You have already supported this comment];
 }
 else{
  var xmlhttp=createxmlhttprequest ();
  var url = action.php Tutorial? action= Top_num&id= "+ID;
  if (id== "") {
   return false;
  
  if (xmlHttp) {
   callback = Getreadystatehandler (Xmlhttp,id);
   xmlhttp.onreadystatechange = callback;
   xmlhttp.open ("Get", url,true);
   xmlhttp.send (NULL);
  }
  setcookie (' get_top_ ' +id+ ' _cookie ', ' istop ', 1);
 }
}

function Getreadystatehandler (xmlhttp,id) {//server returns post-processing functions
var top_num = document.getElementById ("Top_num_" +id). InnerHTML;
return function () {
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
if (xmlhttp.responsetext==1) {
document.getElementById ("Top_num_" +id). Innerhtml=number (Top_num) +1;
}
}
}
}
}

action.php file
PHP is a MySQL tutorial operation, because the simple here the Lord does not put code.

Related Article

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.