Home > Web Front-end > JS Tutorial > Javascript practical tips_javascript skills

Javascript practical tips_javascript skills

WBOY
Release: 2016-05-16 18:30:16
Original
1114 people have browsed it

//自定义的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;
}

//连接数据库

 

//使用数据岛
 



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;">
              

 

 

 

 

//设置光标位置

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 <= n && (found = txt.findText(str)) != 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;
}

//书


//自定义提示条

"#" title="这是提示">tip

//插入文字

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)}" />
<SCRIPT> <br><span style="COLOR: #0000ff">var</span> flash=<span style="COLOR: #ff00ff">""</span>; <br>    WMPVersion= oClientCaps.getComponentVersion(<span style="COLOR: #ff00ff">"{22D6F312-B0F6-11D0-94AB-0080C74C7E95}"</span>,<span style="COLOR: #ff00ff">"ComponentID"</span>);  <br>    <span style="COLOR: #0000ff">if</span> (WMPVersion != <span style="COLOR: #ff00ff">""</span>) { <br>    flash = <span style="COLOR: #ff00ff">""</span>; <br>    <span style="COLOR: #0000ff">var</span> version = WMPVersion.split(<span style="COLOR: #ff00ff">","</span>); <br>    <span style="COLOR: #0000ff">var</span> i; <br>    for (i = 0; i < version.length; i++) { <BR> <SPAN style="COLOR: #0000ff">if</SPAN> (i != 0) <BR> flash += <SPAN style="COLOR: #ff00ff">"."</SPAN>; <BR> flash += version[i]; <BR> } <BR> document.write(<SPAN style="COLOR: #ff00ff">"您的Windows Media Player 版本是:"</SPAN>+flash+<SPAN style="COLOR: #ff00ff">"<p>"); <br>  } <br></SCRIPT>

 

//图象按比例

 


".." 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('

 

 

 //取得innerText 

 

//mergeAttributes 复制所有读/写标签属性到指定元素。

<SCRIPT> <br><span style="COLOR: #0000ff">function</span> fnMerge(){ <br>oSource.children[1].mergeAttributes(oSource.children[0]); <br>} <br></SCRIPT>

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">

<script> <br><span style="COLOR: #0000ff">function</span> CloseOpen() { <br> <span style="COLOR: #0000ff">if</span>(event.clientX<=0 && event.clientY<0) { <br>  alert(<span style="COLOR: #ff00ff">"关闭"</span>); <br> } <br> else <br> { <br>  alert(<span style="COLOR: #ff00ff">"刷新或离开"</span>); <br> } <br>} <br></script>
"CloseOpen()">


获得光标位置


Common methods of TextRange

collapse([bStart])
Collapse the selection and move the insertion point of Range
bStart true(move to the beginning, default) false(move to End)

expand(sUnit)
Expand the selection so that the selection included in the unit is included

findText(sText [, iSearchScope] [, iFlags])
in Range Search sText
iSearchScope starting position, negative direction search
iFlags 1 (search backward) 2 (whole word match) 4 (case sensitive)

moveStart(sUnit [, iCount])
moveEnd(sUnit [, iCount])
Move the beginning or end of the Range
sUnit character(word) word(word) sentence(sentence) textedit(Range)
iCount The number of moves, the default is 1

moveToPoint(iX, iY)
Move the cursor to coordinates (iX, iY)

pasteHTML(sHTMLText)
Replace the html in Range

scrollIntoView([ bAlignToTop])
Scroll to display in the current window
bAlignToTop true(Range is at the beginning of the window) false(Range is at the bottom of the window)

select ()
Let the selection state

  • Event Source Object
    event.srcElement.tagName
    event.srcElement.type
  • Capture release
    event.srcElement.setCapture();
    event.srcElement.releaseCapture();
  • Event Key
    event.keyCode
    event.shiftKey
    event.altKey
    event.ctrlKey
  • Event return value
    event.returnValue
  • Mouse position
    event.x
    event.y
  • Form active element
    document.activeElement
  • Binding events
    document.captureEvents(Event.KEYDOWN);
  • Access form elements
    document.all("txt").focus();
    document.all("txt").select();
  • Form Command
    document.execCommand
  • FORM COOKIE
    document.cookie
  • Menu event
    document.oncontextmenu
  • Create element
    document.createElement("SPAN");
  • Get the element based on the mouse:
    document.elementFromPoint(event.x,event.y).tagName=="TD
    document.elementFromPoint (event.x,event.y).appendChild(ms)
  • Form images
    document.images[index]
  • Form event binding
    document.onmousedown=scrollwindow;
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template