不知道大家注意到我的links页面没有.我写加了搜索评论和查Google PR的功能.今天写个例子实现查PR的例子.基于POST方法.
这个例子用GET方法其实也是很好的.因为传输的数据很少.但是对于一个比较大的表单.项目比较多.输入的字符比较多.那就必须用POST方法了.
下面开始写服务器端查pr的代码.这些代码是我在网上找的,我把它改成我需要的样子.可能不是最好的方法,但是我的要求是能实现查询即可.是在迅雷上找的.源码没有注明作者.这里不好点名感谢了.总之谢谢.哈哈
因为主要介绍js部分这里不贴了.代码到这里下载.
查询页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>查询Google PR</title> <script type="text/javascript" src="checkpr.js"></script> </head> <body> <form onsubmit="YHLCPR.checkPR();return false" id="checkpr_form" method="post" action="http://philna.com/checkpr.php"> <label>输入要查询的网址(URL): <input type="text" name="checkurl" id="checkprurl" tabindex="1" value="" size="55" onmouseover="this.focus();this.select();"/> </label> <input type="submit" name="submit" id="submit" value="查询" tabindex="2" /> </form> </body> </html>
javascript代码.
(function(){ if(!window.YHLCPR) window['YHLCPR']={}; function $(id){ return document.getElementById(id); } function getXmlHttpObject(){ var xmlHttp = null; try{ xmlHttp = new XMLHttpRequest(); }catch(e){ try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function formToRequestString(form_obj){ var query_string=''; var and='&'; for (i=0;i<form_obj.length ;i++ ){ e=form_obj[i]; if (e.name!=''){ if (e.type=='select-one'){ element_value=e.options[e.selectedIndex].value; }else if (e.type=='checkbox' || e.type=='radio'){ if (e.checked==false){ break; }element_value=e.value; }else{ element_value=e.value; } query_string+=and+e.name+'='+element_value; } } return query_string; } function stateChangeListener(){ if(xmlHttp.readyState==1){ document.body.style.cursor = 'wait'; $('submit').disabled=true; }else if(xmlHttp.readyState==4 && xmlHttp.status==200){ alert(xmlHttp.responseText); document.body.style.cursor = 'auto'; setTimeout(function(){$('submit').disabled=false;},1000); }else if(xmlHttp.status!=200 && xmlHttp.readyState==4){ document.body.style.cursor = 'auto'; $('submit').disabled=false; alert('呃 出错了 (建议您刷新浏览器.再试) 服务器返回的错误信息:'+xmlHttp.statusText); } } function checkPR(){ xmlHttp=getXmlHttpObject(); if (xmlHttp == null) { alert ("Oop! Browser does not support HTTP Request.") return; } var url='checkpr.php'; var data=formToRequestString($('checkpr_form')); xmlHttp.onreadystatechange=function(){ stateChangeListener(); } xmlHttp.open("POST", url, true); xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); xmlHttp.send(data); } window['YHLCPR']['checkPR']=checkPR; })()
要注意的是:
貌似是ajax默认只能取得当前主机的东西.是这样的吗?因为我在测试的时候出现了这个问题.所以你用这个例子的时候要php环境了.查询pr的文件到我的Google code那里下载吧.
声明:本站遵循署名-非商业性使用-相同方式共享3.0共享协议. 转载请注明转自 PhilNa ™
居然是沙发!!呵呵!
你看了哪本 JS 的书啊?