您好,欢迎访问一九零五行业门户网

Javascript 实用小技巧_javascript技巧

//自定义的apply,call
function.prototype.apply = function (obj, argu) {
 if (obj) obj.constructor.prototype._caller = this; 
 var argus = new array();
 for (var i=0;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  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(下载完成.);
}
//检验连接是否有效
function getxml(url) 
{
 var xmlhttp = new activexobject(microsoft.xmlhttp);
 xmlhttp.open(get,url, false); 
 try
 { 
  xmlhttp.send();
 }
 catch(e){}
 finally 
 {
  var result = xmlhttp.responsetext;
  if(result) 
  {
   if(xmlhttp.status==200)
   {
    return(true);
   }
   else 
   {
    return(false);
   }
  }
  else 
  {
   return(false);
  }
 }
}
//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;
}
//连接数据库
>
  //用 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 = ;
 shtml +=au_id au_lname au_fname phone address  city state zip
;
  while(!rs.eof)
  {
 shtml +=  + rs(au_id) +   + rs(au_lname) +   + rs(au_fname) +   + rs(phone) +   + rs(address) +   + rs(city) +   + rs(state) +   + rs(zip) + 
;
 rs.movenext;
  }
  shtml += 
;
  document.write(shtml);
  rs.close(); 
  rs = null; 
  conn.close(); 
  conn = null;
//使用数据岛
srno:76>
times:76>
first type=button value= onclick=xmldate.recordset.movefirst()>
prev type=button value= onclick=xmldate.recordset.moveprevious()>  
next type=button value=下一条记录> onclick=xmldate.recordset.movenext()>  
last type=button value=最后一条记录>> onclick=xmldate.recordset.movelast()>   
add type=button value=添加新记录 onclick=xmldate.recordset.addnew()>
xmldate>
20041025-01null
20041101-092004年10月1日2点22分0秒
//获得参数
javascript:location.href=location.href + '?a=1&b=2'>search
>
//可编辑select
width:100px;height:21px;font-size:10pt;>width:18px;border:0px solid red;>r00 style=margin-left:-100px;width:118px; background-color:#ffeeee; onchange=document.all.re_name.value=this.value;>
                1>11111111
                2>222222
                3>333333
//设置光标位置
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;
}
//页内查找
function findinpage(str)
{
 var txt, i, found,n = 0;
 if (str == )
 {
  return false;
 }
 txt = document.body.createtextrange();
 for (i = 0; i false; i++)
 {
  txt.movestart(character, 1);
  txt.moveend(textedit);
 }
 if (found)
 {
  txt.movestart(character, -1);
  txt.findtext(str);
  txt.select();
  txt.scrollintoview();
  n++;  
 }
 else
 {
  if (n > 0)
  {
   n = 0;
   findinpage(str);
  }
  else
  {
   alert(str + ...            您要找的文字不存在。 \n请试着输入页面中的关键字再次查找!);
  }
 }
 return false;
}
//书
http://www.itpub.net/attachment.php?s=&postid=1894598
http://www.wrclub.net/down/listdown.aspx?id=1341 //操作execl
//自定义提示条
# title=这是提示>tip
);
function showpopuptext(){
var o=event.srcelement;
mousex=event.x;
mousey=event.y;
if(o.alt!=null && o.alt!=){o.dypop=o.alt;o.alt=};
        if(o.title!=null && o.title!=){o.dypop=o.title;o.title=};
if(o.dypop!=spop) {
spop=o.dypop;
cleartimeout(curshow);
cleartimeout(tfadeout);
cleartimeout(tfadein);
cleartimeout(tfadewaiting);
if(spop==null || spop==) {
dypoplayer.innerhtml=;
dypoplayer.style.filter=alpha();
dypoplayer.filters.alpha.opacity=0;
}
else {
if(o.dyclass!=null) popstyle=o.dyclass 
else popstyle=cpoptext;
curshow=settimeout(showit(),tpopwait);
}
}
}
function showit(){
dypoplayer.classname=popstyle;
dypoplayer.innerhtml=spop;
popwidth=dypoplayer.clientwidth;
popheight=dypoplayer.clientheight;
if(mousex+12+popwidth>document.body.clientwidth) popleftadjust=-popwidth-24
else popleftadjust=0;
if(mousey+12+popheight>document.body.clientheight) poptopadjust=-popheight-24
else poptopadjust=0;
dypoplayer.style.left=mousex+12+document.body.scrollleft+popleftadjust;
dypoplayer.style.top=mousey+12+document.body.scrolltop+poptopadjust;
dypoplayer.style.filter=alpha(opacity=0);
fadeout();
}
function fadeout(){
if(dypoplayer.filters.alpha.opacitydypoplayer.filters.alpha.opacity+=showpopstep;
tfadeout=settimeout(fadeout(),1);
}
else {
dypoplayer.filters.alpha.opacity=popopacity;
tfadewaiting=settimeout(fadein(),tpopshow);
}
}
function fadein(){
if(dypoplayer.filters.alpha.opacity>0) {
dypoplayer.filters.alpha.opacity-=1;
tfadein=settimeout(fadein(),1);
}
}
document.onmouseover=showpopuptext;
//插入文字
document.onclick =function(){ 
var osource = window.event.srcelement; 
if(osource.tagname!=div) 
return false; 
var sel = document.selection; 
if (sel!=null) { 
var rng = sel.createrange(); 
if (rng!=null) 
rng.pastehtml(插入文字); 


//netscapte下操作xml
doc = new activexobject(msxml2.domdocument);
doc = new activexobject(microsoft.xmldom)
->>
doc = (new domparser()).parsefromstring(sxml,'text/xml')
//判断键值
content-type content=text/html; charset=gb2312>
//javascript document.
请按任意一个键。。。。
//禁止fso
1.注销组件
regsvr32 /u scrrun.dll
2.修改progid 
hkey_classes_root\scripting.filesystemobject
scripting.filesystemobject
3.对于使用object的用户,修改hkey_classes_root\scripting.
//省略号
width: 120px; height: 50px; border: 1px solid blue;
            overflow: hidden; text-overflow:ellipsis>
就是比如有一行文字,很长,表格内一行显示不下.
//检测media play版本
oclientcaps style={behavior:url(#default#clientcaps)} />
//图象按比例
.. onload = drawimage(this)>
//细线select
span style=border:1px solid #000000; position:absolute; overflow:hidden; >
select style=margin:-2px;>
option>1111/option>
option>11111111111111/option>
option>111111111/option>
/select>/span>
//import
function import() {
 for( var i=0; i  var file = arguments[i];
  if ( file.match(/\.js$/i)) 
   document.write('
//js枚举 
  function getcomputername()
{
 var objwmiservice = getobject(winmgmts:root\cimv2);
 for(e = new enumerator(objwmiservice) ; !e.atend() ; e.movenext())
 {
    var getcomputer = e.item();
    return getcomputer.name;
 }
}
//条件编译
//取得innertext
//mergeattributes 复制所有读/写标签属性到指定元素。
id=odiv
attribute1=true
attribute2=true
onclick=alert('click');
onmouseover=this.style.color='#0000ff';
onmouseout=this.style.color='#000000';
>
this is a sample div element.
odiv2>
this is another sample div element.
type=button
value=merge attributes
onclick=fnmerge()
>
将选中内容保存为html
new document 
generator content=editplus>
author content=>
keywords content=>
description content=>
显示版块帖子 
版块帖子分页 
显示单个帖子 
发送新帖 
帖子回复 
个人登陆 
留言的回复功能
save() value=aa>
判断是刷新还是离开
判断是刷新还是关闭-www.51windows.net
content-type content=text/html; charset=gb2312>
author content=51windows,海娃,haiwa>
description content=power by 51windows.net>
closeopen()>
获得光标位置
getcursorposition() onkeyup=getcursorposition()>
北京时间10月6日,世界三大通讯社之一的法新社刊发图文报道,中国国脚孙继海因为在最近的世界杯预选赛中的表现,已经吸引了意大利俱乐部ac米兰和都灵队的争购。中国队只需在10月7日同阿曼队的比赛中战平就将首次进入世界杯决赛圈。图为孙继海(右)1998年12月19日在亚洲杯上的资料图片。
textrange的常用方法
collapse([bstart])
收起选区,并移动range的插入点
bstart true(移到开头,default) false(移到末尾)
expand(sunit)
展开选区,让单位所包括的选区被包含进来
findtext(stext [, isearchscope] [, iflags])
在range中查找stext
isearchscope 开始位置,负数方向搜索
iflags 1(向后搜索) 2(整词匹配) 4(区别大小写)
movestart(sunit [, icount])
moveend(sunit [, icount])
移动range的开头或结尾
sunit character(字) word(词) sentence(句) textedit(range)
icount 移动数量,默认为1
movetopoint(ix, iy)
移动光标到坐标(ix,iy)
pastehtml(shtmltext)
替换range中的html
scrollintoview([baligntotop])
滚动使之在当前窗口显示
baligntotop true(range在窗口开头) false(range在窗口底部)
select()
让选区选中状态
事件源对象
event.srcelement.tagname
event.srcelement.type 捕获释放
event.srcelement.setcapture(); 
event.srcelement.releasecapture();  事件按键
event.keycode
event.shiftkey
event.altkey
event.ctrlkey 事件返回值
event.returnvalue 鼠标位置
event.x
event.y 窗体活动元素
document.activeelement 绑定事件
document.captureevents(event.keydown); 访问窗体元素
document.all(txt).focus();
document.all(txt).select(); 窗体命令
document.execcommand 窗体cookie
document.cookie 菜单事件
document.oncontextmenu 创建元素
document.createelement(span);  根据鼠标获得元素:
document.elementfrompoint(event.x,event.y).tagname==td
document.elementfrompoint(event.x,event.y).appendchild(ms)  窗体图片
document.images[索引] 窗体事件绑定
document.onmousedown=scrollwindow;
其它类似信息

推荐信息