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.