欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Baidu Musicbox 用到的ajax代码

程序员文章站 2023-12-24 08:11:21
var names; var cs, st; var rorw=""; var lrct; function g...
var names;
var cs, st;
var rorw="";
var lrct;

function getname(songname){
    if(songname==""){
        names=$("name").value;
    }else{
        names=songname;
    }
    geturl();
}
function geturl(){
    ajaxcall("inc/songurl.asp?type=url1&keyword="+names,"musiclist","isxml=true");
    addbox(names);
}
function top(){
    ajaxcall("inc/songlist.asp?ac=new","toplist","isxml=true");
}
function toplist(doc){
    var items=getelementsbyclass(doc,"item");
        var linker=new array();
        var linkers=new array();
    var tmp="<ul>";
    for(var i=0;i<items.length;i++){
                if(gettagvalue(items[i], "singer")=="")
                {
                  linker[i]="";
                  linkers[i]="";   
                }
                else
                {
                  linker[i]="("+gettagvalue(items[i], 'singer').replace(/ /g," ")+")";
                  linkers[i]=" "+gettagvalue(items[i], 'singer').replace(/ /g," ");
                }
        var d=i+1;
        tmp+='<li>'+d+'. <a href=javascript:getname("'+gettagvalue(items[i], 'songname').replace(/ /g," ")+linkers[i]+'") title="'+gettagvalue(items[i], 'songname').replace(/ /g," ")+linker[i]+'">'+gettagvalue(items[i], 'songname').replace(/ /g," ")+linker[i]+'</a></li>';
    }
    tmp+="</ul>";
    $("toplist").innerhtml=tmp;
}
function s2(){
    ajaxcall("inc/songlist.asp?ac=all","s3","isxml=true");
}
function s3(doc){
    var items=getelementsbyclass(doc,"item");
        var linker=new array();
        var linkers=new array();
    var tmp="<ul>";
    for(var i=0;i<items.length;i++){
                if(gettagvalue(items[i], "singer")=="")
                {
                  linker[i]="";
                  linkers[i]="";
                }
                else
                {
                  linker[i]="("+gettagvalue(items[i], 'singer').replace(/ /g," ")+")";
                  linkers[i]=" "+gettagvalue(items[i], 'singer').replace(/ /g," ");
                }
        var d=i+1;
        tmp+='<li>'+d+'. <a href=javascript:getname("'+gettagvalue(items[i], 'songname').replace(/ /g," ")+linkers[i]+'") title="'+gettagvalue(items[i], 'songname').replace(/ /g," ")+linker[i]+'">'+gettagvalue(items[i], 'songname').replace(/ /g," ")+linker[i]+'</a></li>';
    }
    tmp+="</ul>";
    $("topalllist").innerhtml=tmp;
}
function musiclist(doc){
    var items=getelementsbyclass(doc,"item");
    var tmp="<ul>";
    for(var i=0;i<22 && i<items.length;i++){
        var d=i+1;
        type=gettagvalue(items[i], 'type');
        tmp+='<li> <img src="img/list.gif" /> <a title="试听点 '+d+' ('+type+')" href=javascript:play("'+gettagvalue(items[i], 'type')+'","'+gettagvalue(items[i], 'link').replace(/ /g,' ')+'")>'+names.replace(/ /g,' ')+' ('+type+')</a></li>';
    }
    tmp+="</ul>";
    $("list").innerhtml=tmp;
    $("msg").style.display="none";
    play(gettagvalue(items[0], "type"),gettagvalue(items[0], "link"));
}
function play(obj,url){
        initlrc();
    $("guanxin").style.display="none";
    $("msg").style.display="block";
    ajaxcall("inc/songurl.asp?type=url2&keyword="+url+"&attach="+obj,"gotoplay","isxml=true");
    ajaxcall("inc/lyrics.asp?song="+names,"getlrc","isxml=true");
}
var tl;
function getlrc(doc){
    tl="";
    var items=getelementsbyclass(doc,"item");
    var tmp="<div id=lrcdiv class=lrcdiv>";
    for(var i=0;i<items.length;i++){
        tmp+='<div class=div id=t_'+gettagvalue(items[i], 'time')+' onclick=to(\''+gettagvalue(items[i], 'time')+'\')>'+gettagvalue(items[i], 'lrc')+'</div>';
        tl+=gettagvalue(items[i], 'time')+"|";
    }
    tmp+="</div>";
    $("lrc").innerhtml=tmp;
}

function gotoplay(docs){
    var type=gettagvalue(docs, "type");
    var link=gettagvalue(docs, "link");
    if(type=="rm" || type=="ra" || type=="ram" || type=="rmvb" || type=="mpga" || type=="mp4" || type=="3gp")
      $("players").innerhtml=realplayer(link);
    else
      $("players").innerhtml=mdplayer(link);
    var link3=cuturl(link);
    $("msg").style.display="none";
    $("wmaurl").innerhtml="歌曲出处: <a href="+link.replace(/ /g," ")+" target='_blank' title='点右键另存:"+names.replace(/ /g," ")+"'>"+link3.replace(/ /g," ")+"</a>";
    settimeout(guanxin,1000);
    playobj();
}

function initlrc(){
        cleartimeout(lrct);
        rorw="";
        $("players").innerhtml="";
        $("lrc").innerhtml="";
    st=0;
        cs=0;
}

function playobj(){
        if($("mplayer"))
        {
          rorw="wmp";
          ldl();
        }
        else if($("rplayer"))
        {
          rorw="rmp";
          try { rplayer.setwanterrors(true); } catch(hh){}
          ldl();
        }
        else
          playobj();
}

function guanxin(){
    $("guanxin").style.display="block";
    settimeout(guanxin2,10000);
}
function guanxin2(){
    $("guanxin").style.display="none";
}

var player;
function mdplayer(str){
player='<object classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" id="mplayer" width="100%" height="64"><param name="url" value="'+str+'"><param name="enablecontextmenu" value="0"><param name="enableerrordialogs" value="0"></object>';
return player;
}

function realplayer(str){
player='<object classid="clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa" id="rplayer" width="100%" height="60"><param name="src" value="'+str+'"><param name="controls" value="controlpanel,statusbar"><param name="autostart" value="true"></object>';
return player;
}

function cuturl(song_url){
    var len=getstractuallen(song_url);
    var max_disp_len=40;
    if(len<=max_disp_len) return song_url;
    var disp_song_url=song_url;
    var pos1=song_url.indexof("://");
    if(pos1>0){
        disp_song_url=song_url.substring(pos1+3,len);
        var pos2=disp_song_url.indexof("/");
        if(pos2>0) disp_song_url=song_url.substring(0,pos1+pos2+4);
        var len2=max_disp_len-3-getstractuallen(disp_song_url);
        if(len2>0) disp_song_url=disp_song_url+'...'+song_url.substring(len-len2,len);
        return disp_song_url;
    }
}

function getstractuallen(schars){
    return schars.replace(/[^\x00-\xff]/g,"xx").length;
}

function ajaxcall(url,handleresponse,param){
    var isxml=false;//false:html or true:xml
    var iscache=false;//false:no-cache or true:cache
    var method="get";//get or post
    var attach="";
    if(param){
        re = / /g; 
        param=param.replace(re, "");
        var tmp=param.split(",");
        for(var i=0;i<tmp.length;i++){
            var pa=tmp[i].substr(0,3);
            var ct=tmp[i].split("=")[1];
            switch (pa){
                case "isx":
                if(ct=="true"){
                    isxml= true; 
                }else{
                    isxml= false; 
                }
                break;
                case "isc":
                if(ct=="true"){
                    iscache= true; 
                }else{
                    iscache= false; 
                }
                break;
                case "met":
                method=ct;
                break;
                case "att":
                attach=ct;
                break;
            }
        }
    }
    if(!iscache){
        var t=new date().gettime();
        if(url.indexof("?")>0){
        url+="&t="+t;
        }else{
            url+="?t="+t;
        }
    }    
    var postdata=null;
    if(method=="post"){
        purl=url.split("?");
        url=purl[0];
        postdata=purl[1];
    }
    requesthttp(url,handleresponse,method,isxml,postdata,attach);
}

function requesthttp(burl,rdrandleresponse,bmethod,bxml,formdata,attach){
    var http_request = false;
    function dohttprequest(){    
        if (window.xmlhttprequest){
            http_request = new xmlhttprequest();
            if (http_request.overridemimetype){
                http_request.overridemimetype('text/xml');
            }
        } else if (window.activexobject){
            try{
                http_request = new activexobject("msxml2.xmlhttp");
            } catch (e){
                try{
                    http_request = new activexobject("microsoft.xmlhttp");
                } catch (e){}
            }
        }
        if (!http_request){
            alert('giving up :( cannot create an xmlhttp instance');
            return false;
        }
        http_request.onreadystatechange=handlestatechange;    
        http_request.open(bmethod,burl,true);
        if(formdata){
            http_request.setrequestheader("content-length",formdata.length);   
            http_request.setrequestheader("content-type","application/x-www-form-urlencoded");
            http_request.send(formdata);
        }else{
            http_request.setrequestheader("content-type","text/html");
            http_request.send(null);
        }
    }
    function handlestatechange(){
        if(http_request.readystate==4 && http_request.status==200){
            var response_content;
            if(bxml){
                response_content=http_request.responsexml;
            }else{
                response_content=http_request.responsetext;    
            }        
            if(typeof rdrandleresponse == "function"){
                rdrandleresponse(response_content, attach);
            }else{
                eval(rdrandleresponse+"(response_content,attach)");
            }
        }
    }
    dohttprequest();
}

function gettagvalue(doc, tag){
     var elems = doc.getelementsbytagname(tag)[0].firstchild.nodevalue;
     return elems; 
}

function $(obj){
  return document.getelementbyid(obj);    
}

function getelementsbyclass(node,tag){
    var els = node.getelementsbytagname(tag);
    return els;
}
歌词函数:
复制代码 代码如下:

function setcookie(name,value){
    var days = 30;
    var exp = new date();
    exp.settime(exp.gettime() + days*24*60*60*1000);
    document.cookie = name + "="+ escape(value) +";expires="+ exp.togmtstring();
}
function getcookie(name){
    var arr = document.cookie.match(new regexp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;
}
setcookie("box",getcookie("box"));
function addbox(name){
    var a1=name+"|*|";
    var box=getcookie("box");
    box=box.replace(a1,"");
    var value=a1+box;
    setcookie("box",value);
    listbox();
}
function delbox(name){
    var del=name+"|*|";
    var s=getcookie("box");
    s = s.replace(del, "");
    setcookie("box",s);
    listbox();
}
function listbox(){
    var tmp="";
    var s2=getcookie("box");
    var s3=s2.split("|*|");
    var s4=s3.length-1;
        if(s4>8) $("boxlist").style.height="139px";
    for(var i=0;i<s4;i++){
        tmp+="<div id=\"box\"><div id=\"del\" style=\"float: right;\"><span onclick=javascript:delbox('"+s3[i]+"')>删除</span></div><div id=\"boxtitle\"><a href=javascript:getname('"+s3[i]+"')>"+s3[i]+"</a></div></div>";
    }
    $("boxlist").innerhtml=tmp;
}

function getebid(id){return document.getelementbyid(id);}
function to(t){
        if(rorw=="") return;
    if(rorw=="rmp" && rplayer.getplaystate()==3) rplayer.setposition(t*1000);
    if(rorw=="wmp" && mplayer.playstate==3) mplayer.controls.currentposition=t;
}

function lrcrun(){
        var rtl="|"+tl;
        if((rtl.indexof("|"+cs+"|")) != -1){
            getebid("lrcdiv").scrolltop=getebid("lrcdiv").offsettop-parseint(getebid("lrcdiv").offsetheight/2)+getebid("t_"+cs).offsettop+10;
            getebid("t_"+st).classname="";
            getebid("t_"+cs).classname="b";
            st=cs;
        }
}

function ldl(){
    if(rorw=="rmp" && rplayer.getplaystate()==3) {cs=math.floor(rplayer.getposition()/1000); lrcrun();}
    if(rorw=="wmp" && mplayer.playstate==3)    {cs=math.floor(mplayer.controls.currentposition); lrcrun();}
    lrct=settimeout("ldl()", 10);
}

上一篇:

下一篇: