JavaScript |
|
<返回>
2.讓TextArea自動換行
使用TextArea的Wrap屬性
|
ASP中手動換行用: replace(rs("A"),"</br>", VBCRLF)
str=request.querystring("text1")
str=Replace(str, Chr(32), " ")
'把空格換成 標誌
str=Replace(str, vbCrLf, "<br>")
'把回車換行符換成<br>標誌
調出時再轉過來就好了
<返回>
3.讓TextArea支持Table鍵
<SCRIPT LANGUAGE="JavaScript">
<!--
function testTab()
{
var sel = document.selection.createRange();
var mytext= sel.text;
var i,j,k;
if (event.shiftKey && event.keyCode == 9)
{
arr=mytext.split(String.fromCharCode(13,10))
mytext=""
for(k=0;k<arr.length;k++)
{
for (j=1; j<=4;j++)
{
if(arr[k].substr(0,1)=="/u0020")
{
arr[k]=arr[k].slice(1)
}
}
mytext += arr[k] + ((k==(arr.length-1))? "" : String.fromCharCode(13,10));
}
with(sel){
sel.text = mytext;
collapse(true)
moveEnd("character",0)
moveStart("character",(mytext.length ) * -1)
select()
}
window.event.cancelBubble = true;
event.returnValue = false;
return;
}
if (event.keyCode == 9)
{
arr=mytext.split(String.fromCharCode(13,10))
mytext=""
for(j=0;j<arr.length;j++)
{
mytext += "/u0020/u0020/u0020/u0020" + arr[j] + ((j==(arr.length-1))? "" : String.fromCharCode(13,10));
}
with(sel){
sel.text = mytext;
collapse(true)
moveEnd("character",0)
moveStart("character",(mytext.length -4) * -1)
select()
}
window.event.cancelBubble = true;
event.returnValue = false;
return;
}
}
//-->
</SCRIPT>
<返回>
4.複製數據到剪貼板
JM_cc(txt)
window.clipboardData.setData("Text", txt);
global.focus();
document.execCommand('Paste');
window.clipboardData.setData("Text", old);
<返回>
5.得到當前選中的文本
var sel = document.selection.createRange();
var mytext= sel.text; //當前選中的文本
var e = event.srcElement; //設置文本爲選中
var r =e.createTextRange();
r.moveStart('character',e.value.length);
r.collapse(true);
r.select();
<返回>
6.客戶端腳本的基本對象
瀏覽器對象 |
<返回>
7.保護自己編寫的HTML和腳本的方法
(1). οncοntextmenu="window.event.returnValue=false" 將徹底屏蔽鼠標右鍵
<table border οncοntextmenu=return(false)><td>no</table> 可用於Table
(2). <body onselectstart="return false"> 取消選取、防止複製
(3). οnpaste="return false" 不準粘貼
(4). οncοpy="return false;" oncut="return false;" 防止複製
(5). 防止被人frame
<SCRIPT LANGUAGE=JAVASCRIPT><!--
if (top.location != self.location)top.location=self.location;
// -->
</SCRIPT>
(6). 永遠都會帶着框架
<script language="JavaScript"><!--
if (window == top)top.location.href = "frames.htm"; //frames.htm爲框架網頁
// --></script>
(7).網頁將不能被另存爲
<noscript><iframe src=*.html></iframe></noscript>
<返回>
8.IE地址欄前換成自己的圖標
<link rel="Shortcut Icon" href="favicon.ico"> _fcksavedurl=""favicon.ico">"
<返回>
9.可以在收藏夾中顯示出你的圖標
<link rel="Bookmark" href="favicon.ico"> _fcksavedurl=""favicon.ico">"
<返回>
10.關閉輸入法
<input style="ime-mode:disabled">
<返回>
11.直接查看源代碼
<input type=button value=查看網頁源代碼 οnclick="window.location = 'view-source:'+ 'http://www.csdn.net/'">
<返回>
12.在Javascript中定義一個對象(屬性,方法)
function pasta(grain, width, hasEgg) {
this.grain = grain;
this.width = width;
this.hasEgg = hasEgg;
this.toString = pastaToString;
}
function pastaToString() {
return "Grain: " + this.grain + "/n" + "Width: " + this.width + "/n" + "Egg?: " + Boolean(this.hasEgg);
}
var P1=new pasta(3,3,false);
<返回>
13. 取得控件的絕對位置
//Javascript
<script language="Javascript">
function getIE(e){
var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
alert("top="+t+"/nleft="+l);
}
</script>
<返回>
14. 光標是停在文本框文字的最後
<script language="javascript">
function cc()
{
var e = event.srcElement;
var r =e.createTextRange();
r.moveStart('character',e.value.length);
r.collapse(true);
r.select();
}
</script>
<input type=text name=text1 value="123" οnfοcus="cc()">
<返回>
15. 判斷上一頁的來源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer
<返回>
16. 最小化、最大化、關閉窗口
<object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize"></object>
<object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize"></object>
<OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM NAME="Command" VALUE="Close"></OBJECT>
<input type=button value=最小化 οnclick=hh1.Click()>
<input type=button value=最大化 οnclick=hh2.Click()>
<input type=button value=關閉 οnclick=hh3.Click()>
本例適用於IE
<返回>
17. 檢查一段字符串是否全由數字組成
<script language="Javascript"><!--
function checkNum(str){return str.match(//D/)==null}
// --></script>
<返回>
18. 獲得一個窗口的大小
document.body.clientWidth,document.body.clientHeight
<返回>
19. 怎麼判斷是否是字符
if (/[^/x00-/xff]/g.test(s))
alert("含有漢字");
else
alert("全是字符");
<返回>
20. 檢測某個網站的鏈接速度
把如下代碼加入<body>區域中:
<script language=Javascript>
tim=1
setInterval("tim++",100)
b=1
var autourl=new Array()
autourl[1]="www.njcatv.net"
autourl[2]="javacool.3322.net"
autourl[3]="www.sina.com.cn"
autourl[4]="www.nuaa.edu.cn"
autourl[5]="www.cctv.com"
function butt(){
document.write("<form name=autof>")
for(var i=1;i<autourl.length;i++)
document.write("<input type=text name=txt"+i+" size=10 value=測試中……> =》<input type=text name=url"+i+" size=40> =》<input type=button value=GO οnclick=window.open(this.form.url"+i+".value)><br/>")
document.write("<input type=submit value=刷新></form>")
}
butt()
function auto(url){
document.forms[0]["url"+b].value=url
if(tim>200)
{
document.forms[0]["txt"+b].value="鏈接超時"
}
else
{
document.forms[0]["txt"+b].value="時間"+tim/10+"秒"
}
b++
}
function run()
{
for(var i=1;i<autourl.length;i++)
document.write("<img src=http://"+autourl[i]+"/"+Math.random()+" width=1 height=1 οnerrοr=auto('http://"+autourl[i]+"')>")
}
run()</script>
<返回>
21. 各種樣式的光標
auto :標準光標
default :標準箭頭
hand :手形光標
wait :等待光標
text :I形光標
vertical-text :水平I形光標
no-drop :不可拖動光標
not-allowed :無效光標
help :?幫助光標
all-scroll :三角方向標
move :移動標
crosshair :十字標
e-resize
n-resize
nw-resize
w-resize
s-resize
se-resize
sw-resize
<返回>
22.讓TEXTAREA自適應文字的行數
<textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight"></textarea>
<返回>
23. 日期減去天數等於第二個日期
<script language=Javascript>
function cc(dd,dadd)
{
//可以加上錯誤處理
var a = new Date(dd)
a = a.valueOf()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(a)
alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
}
cc("12/23/2002",2)
</script>
<返回>
24. 選擇了哪一個Radio
<HTML>
<script language="vbscript">
function checkme()
for each ob in radio1
if ob.checked then
window.alert ob.value
next
end function
</script>
<BODY>
<INPUT name="radio1" type="radio" value="style" checked>Style
<INPUT name="radio1" type="radio" value="barcode">Barcode
<INPUT type="button" value="check" οnclick="checkme()">
</BODY></HTML>
<返回>
25.獲得本頁url的request.servervariables("")集合
Response.Write "<TABLE border=1><!-- Table Header --><TR><TD><B>Variables</B></TD><TD><B>Value</B></TD></TR>"
for each ob in Request.ServerVariables
Response.Write "<TR><TD>"&ob&"</TD><TD>"&Request.ServerVariables(ob)&"</TD></TR>"
next
Response.Write "</TABLE>"
<返回>
26.ENTER鍵可以讓光標移到下一個輸入框
<input οnkeydοwn="if(event.keyCode==13)event.keyCode=9">
<返回>
28.引用其他網頁
<table width=100% border="0"><tr><td><script language="JavaScript" location="http://91down.7161.net" id="nd91down" src="http://91down.7161.net/js/new1-1.htm"></script></td><td><script language="JavaScript" location="http://91down.7161.net" id="nd91down" src="http://91down.7161.net/js/new1-2.htm"></script></td></tr></table>
<返回>
29.常用的正則表達式
匹配中文字符的正則表達式: [/u4e00-/u9fa5]
匹配雙字節字符(包括漢字在內):[^/x00-/xff]
匹配空行的正則表達式:/n[/s| ]*/r
匹配HTML標記的正則表達式:/<(.*)>.*<///1>|<(.*) //>/
匹配首尾空格的正則表達式:(^/s*)|(/s*$)
匹配Email地址的正則表達式:/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*
匹配網址URL的正則表達式:http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?
(1)應用:計算字符串的長度(一個雙字節字符長度計2,ASCII字符計1)
String.prototype.len=function(){return this.replace([^/x00-/xff]/g,"aa").length;}
(2)應用:javascript中沒有像vbscript那樣的trim函數,我們就可以利用這個表達式來實現
String.prototype.trim = function()
{
return this.replace(/(^/s*)|(/s*$)/g, "");
}
(3)應用:利用正則表達式分解和轉換IP地址
function IP2V(ip) //IP地址轉換成對應數值
{
re=/(/d+)/.(/d+)/.(/d+)/.(/d+)/g //匹配IP地址的正則表達式
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}
(4)應用:從URL地址中提取文件名的javascript程序
s="http://www.9499.net/page1.htm";
s=s.replace(/(.*//){0,}([^/.]+).*/ig,"$2") ; //Page1.htm
(5)應用:利用正則表達式限制網頁表單裏的文本框輸入內容
用正則表達式限制只能輸入中文:οnkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/u4E00-/u9FA5]/g,''))"
用正則表達式限制只能輸入全角字符: οnkeyup="value=value.replace(/[^/uFF00-/uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/uFF00-/uFFFF]/g,''))"
用正則表達式限制只能輸入數字:οnkeyup="value=value.replace(/[^/d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"
用正則表達式限制只能輸入數字和英文:οnkeyup="value=value.replace(/[/W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"
<返回>
30.彈出來提示對話框
window.showModalDialog(url);
<返回>
31.取得查詢字符串,然後就生成成對的數組
var argstr = window.top.location.search.substring(1,window.top.location.search.length);
var args = argstr.split('&');
<返回>
32.另類的onload函數
<SCRIPT FOR=window event=onload>
try
{
Start();
}
catch (exception)
{
}
</script>
<返回>
33.取得IE的版本
var ieVer = parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf("MSIE ")+5, navigator.appVersion.length))
var isIE6 = ieVer >= 6.0
<返回>
34.提交表單
// aimForm 爲表單名 aimPage爲提交到的頁
//提交表單到新建的網頁
function SubmitFormToNewPage(aimForm,aimPage){
aimForm.method="POST";
aimForm.target="_blank";
aimForm.action=aimPage;
aimForm.submit();
}
//在本地提交表單
function SubmitFormToLocalPage(aimForm,aimPage){
aimForm.method="POST";
aimForm.target="_self";
aimForm.action=aimPage;
aimForm.submit();
}
<返回>
35.判斷是否是整數
function IsNum(s) //整數
{
if(s=="null"||s=="undefined"||s.length<1)
return false;
if(isNaN(parseInt(s)))
return false;
else
if((parseInt(s)+"").length!=s.length)
return false;
else
return true;
}
function IsNumber(JudgeNum){ //判斷大於0的數
var JudgeStr=JudgeNum.toString();
for (var i=0;i<JudgeStr.length;i++) {
var oneChar=JudgeStr.charAt(i);
if (oneChar<"0" || oneChar >"9"){
return false;
}
}
return true;
}
<返回>
36.鏈接css文件和js文件
<link rel="stylesheet" href="../css/style.css" _fcksavedurl=""../css/style.css"" type="text/css">
<script language="javascript" src="../includes/jslib.js" ></script>
<返回>
37.引用框架的內容
window.top.frames["mainFrame"].location.href=s;
在IFRAME標籤中引用框架的內容
parent.frames["mainFrame"].location.href
在窗口中引用IFrame中的內容
window.top.frames["mainFrame"].confFrame.location.href
<返回>
38.打開沒有最大化按鈕的窗口
window.open("http://www.google.com","","width=250,height=220,scrollbars=no,resizable=no,center=yes");
<返回>
39.在頁面上顯示一個有邊框的Frame
<fieldset style="width:500;height:100">
<legend>標題</legend>
具體內容
</fieldset>
<返回>
40.判斷日期1是不是大於日期2
function IsDate1AfterThanDate2(year1,month1,day1,year2,month2,day2){
var iFrom=Date.parse(month1+"-"+day1+"-"+year1);
var iTo=Date.parse(month2+"-"+day2+"-"+year2);
if(iFrom>iTo)
return true;
else
return false;
}
function IsDate(year,month,day){
if( (!IsNumber(year))||(year.length!=4))
return false;
if( (!IsNumber(month))||(month>12)||(month<1) )
return false;
if( (!IsNumber(day))||(day>31)||(day<1) )
return false;
var myDate=new Date();
myDate.setFullYear(year,month-1,day);
if (isNaN(myDate.getFullYear())){
return false;
}
else{
if( (myDate.getFullYear()!=year)||(myDate.getDate()!=day)||(myDate.getMonth()!=(month-1).toString()) )
return false;
}
return true;
}
function IsNumber(JudgeNum){
var JudgeStr=JudgeNum.toString();
for (var i=0;i<JudgeStr.length;i++) {
var oneChar=JudgeStr.charAt(i);
if (oneChar<"0" || oneChar >"9"){
return false;
}
}
return true;
}
<返回>
41.常用的Javascript內建類的方法
|
<返回>
42.如何在另一個窗體對原來窗體進行操作
(1)對用window.open打開的新窗體,用window.opener來訪問原來窗體的對象。例如重新加載頁面 window.opener.location.reload(); 可以通過window.opener.varName 來訪問原變量。
(2)對用showModalDialog打開的新窗體
(主頁面) var returnvalue=window.showModalDialog('2.htm',this,"status=no; help=no; dialogWidth=330px; dialogHeight=305px;"); //第二個參數傳入本窗體
(子頁面) window.dialogArguments.varName 來訪問原變量
window.returnValue=*** 來設定returnvalue的值
<返回>
43.層的隱藏與顯示
只要設置style的display屬性即可。比如<div style="display:none" id="MyDiv">隱藏的層</div>
(1)如果要顯示它可以通過腳本來控制 window.document.getElementById("MyDiv").style.display = "";
(2)如果要隱藏它可以通過腳本來控制 window.document.getElementById("MyDiv").style.display = "none";
<返回>
44.禁止右鍵
<body οncοntextmenu="return false">
<返回>
45.屏蔽頁面中程序運行出錯信息
window.onerror = function() { return true; } |
try{ ...... } catch(e){} |
<返回>
46.得到當前顯示器的分辨率
window.srceen.width 得到屏幕的寬度
window.srceen.height 得到屏幕的高度
如果當前分辨率爲800*600,window.srceen.width是800,window.srceen.height是600
<返回>
47.定時運行特定代碼
setTimeout(Code,Timeout);
setInterval(Code,Timeout);
Code是一段字符串,裏邊是js代碼,Timeout是時間間隔,單位是微秒
setTimeout是從現在算起多少微秒後運行該代碼(只運行一次)
setInterval是每隔多少微秒運行一次代碼
<返回>
48.得到本頁網址
var Url = window.location.href;
<返回>
49.保存當前頁面的內容
document.execCommand("SaveAs","","C://index.htm");
//execCommand實際上是一個功能強大的函數,一般的編輯器都是用其實現。詳見 http://www.51js.com/viewthread.php?tid=13450&fpage=1&highlight=execCommand
<返回>
50.刷新當前頁面
window.location.reload();
<返回>
51.獲得當前日期
var d; d = new Date();
alert (d.getMonth() + 1); // 獲取月份。
alert (d.getDate()); // 獲取日。
alert (d.getYear()); // 獲取年份。
<返回>