java技术,博客,论坛 |
Javascript 小技巧全集(5) |
//自定义的apply,call Function.prototype.apply = function (obj, argu) {
if (obj) obj.constructor.prototype._caller = this; var argus = new Array(); for (var i=0;i<argu.length;i++) argus[i] = "argu[" + i + "]"; var r; eval("r = " + (obj ? ("obj._caller(" + argus.join(",") + ");") : ("this(" + argus.join(",") + ");"))); return r; }; Function.prototype.call = function (obj) { var argu = new Array(); for (var i=1;i<arguments.length;i++) argu[i-1] = arguments[i]; return this.apply(obj, argu); };
function DownURL(strRemoteURL,strLocalURL)
{ try { var xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP"); xmlHTTP.open("Get",strRemoteURL,false); xmlHTTP.send(); var adodbStream=new ActiveXObject("ADODB.Stream"); adodbStream.Type=1;//1=adTypeBinary adodbStream.Open(); adodbStream.write(xmlHTTP.responseBody); adodbStream.SaveToFile(strLocalURL,2); adodbStream.Close(); adodbStream=null; xmlHTTP=null; } catch(e) { window.confirm("下载URL出错!"); } //window.confirm("下载完成."); } //POST代替FORM
//readyState是xmlhttp返回数据的进度,0=载入中,1=未初始化,2=已载入,3=运行中,4=完成 //组件是否安装 //检查网页是否存在 function CheckURL(URL)
{ var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.Open("GET",URL, false); try { xmlhttp.Send(); var result = xmlhttp.status; } catch(e) {return(false); } if(result==200) { return true; } xmlhttp = null; return false; } //连接数据库 <script language="javascript">
//用 JavaScript 写服务器端连接数据库的代码示例 var conn = new ActiveXObject("ADODB.Connection"); conn.Open("Provider=SQLOLEDB.1; Data Source=localhost; User ID=sa; " +"Password=; Initial Catalog=pubs"); var rs = new ActiveXObject("ADODB.Recordset"); var sql="select * from authors"; rs.open(sql, conn); shtml = "<table width='100%' border=1>"; shtml +="<tr bgcolor='#f4f4f4'><td>au_id</td><td>au_lname</td><td>au_fname</td><td>phone</td><td>address</td><td> city</td><td>state</td><td>zip</td></tr>"; while(!rs.EOF) { shtml += "<tr><td>" + rs("au_id") + "</td><td>" + rs("au_lname") + "</td><td>" + rs("au_fname") + "</td><td>" + rs("phone") + "</td><td>" + rs("address") + "</td><td>" + rs("city") + "</td><td>" + rs("state") + "</td><td>" + rs("zip") + "</td></tr>"; rs.moveNext; } shtml += "</table>"; document.write(shtml); rs.close(); rs = null; conn.close(); conn = null; </script> //使用数据岛 //获得参数
//可编辑SELECT
//设置光标位置 function getCaret(textbox)
{ var control = document.activeElement; textbox.focus(); var rang = document.selection.createRange(); rang.setEndPoint("StartToStart",textbox.createTextRange()) control.focus(); return rang.text.length; } function setCaret(textbox,pos) { try { var r =textbox.createTextRange(); r.moveStart('character',pos); r.collapse(true); r.select(); } catch(e) {} } function selectLength(textbox,start,len) { try { var r =textbox.createTextRange(); r.moveEnd('character',len-(textbox.value.length-start)); r.moveStart('character',start); r.select(); } catch(e) {//alert(e.description)} } function insertAtCaret(textbox,text) { textbox.focus(); document.selection.createRange().text = text; } |
共7页: 上一页 [1] [2] [3] [4] [5] [6] [7]下一页 |
Copyright (C) 2004 http://www.onejava.com, Java技术联盟. All Rights Reserved