JS常用代碼收集2

以下爲摘自網絡的JS代碼,供日後學習和查閱(所有內容摘自網絡,部分原地址在文末尾):

如內容超出單元格,則隱藏
style="TABLE-LAYOUT: fixed"

讓彈出窗口總是在最上面: <body οnblur="this.focus();">
不要滾動條? 讓豎條沒有: <body style='overflow:scroll;overflow-y:hidden'> </body>
讓橫條沒有: <body style='overflow:scroll;overflow-x:hidden'> </body>
兩個都去掉?更簡單了 <body scroll="no"> </body>
怎樣去掉圖片鏈接點擊後,圖片周圍的虛線? <a href="#" onFocus="this.blur()"><img src="logo.jpg" border=0></a>
電子郵件處理提交表單 <form name="form1" method="post" action="mailto:****@***.com " enctype="text/plain"> <input type=submit> </form>
在打開的子窗口刷新父窗口的代碼裏如何寫? window.opener.location.reload()
如何設定打開頁面的大小 <body οnlοad="top.resizeTo(300,200);">
在頁面中如何加入不是滿鋪的背景圖片,拉動頁面時背景圖不動 <html><head> <STYLE> body {background-image:url(logo.gif); background-repeat:no-repeat; background-position:center } </STYLE> </head> <body bgproperties="fixed" > </body> </html>

各種樣式的光標 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

本機ip<%=request.servervariables("remote_addr")%>
服務器名<%=Request.ServerVariables("SERVER_NAME")%>
服務器IP<%=Request.ServerVariables("LOCAL_ADDR")%>
服務器端口<%=Request.ServerVariables("SERVER_PORT")%>
服務器時間<%=now%> IIS
版本<%=Request.ServerVariables"SERVER_SOFTWARE")%>
腳本超時時間<%=Server.ScriptTimeout%>
本文件路徑<%=server.mappath(Request.ServerVariables("SCRIPT_NAME"))%>
服務器CPU數量<%=Request.ServerVariables("NUMBER_OF_PROCESSORS")%>
服務器解譯引擎<%=ScriptEngine & "/"& ScriptEngineMajorVersion &"."&ScriptEngineMinorVersion&"."& ScriptEngineBuildVersion %>
服務器操作系統<%=Request.ServerVariables("OS")%>

文本豎排方式
<style type="text/css">
<!--
.shupai {Writing-mode:tb-rl}
-->
</style>
超鏈接去虛線邊框
在鏈接中加上οnfοcus="this.blur()"

網頁搜索關鍵字 頭裏插入
<META NAME="keywords" CONTENT="xxxx,xxxx,xxx,xxxxx,xxxx,">

收藏夾圖標
<link rel = "Shortcut Icon" href="favicon.ico">

我的電腦
file:///::{20D04FE0-3AEA-1069-A2D8-08002B30309D }
網上鄰居
file:///::%7B208D2C60-3AEA-1069-A2D7-08002B30309D%7D
我的文檔
file:///::%7B450D8FBA-AD25-11D0-98A8-0800361B1103%7D
控制面板
file:///::{20D04FE0-3AEA-1069-A2D8-08002B30309D}/::{21EC2020-3AEA-1069-A2DD-08002B30309D }
回收站
file:///::%7B645FF040-5081-101B-9F08-00AA002F954E%7D

鼠標控制圖片隱現效果
把如下代碼加入<body>區域中:
<SCRIPT language="javascript">
<!--
function makevisible(cur,which){
if (which==0)
cur.filters.alpha.opacity=100
else
cur.filters.alpha.opacity=20
}
//-->
</SCRIPT>
  2、把如下代碼加入<body>區域中:
<img src="2.gif" style="filter:alpha(opacity=20)"
onMouseOver="makevisible(this,0)"
onMouseOut="makevisible(this,1)">

禁止圖片下載
<A HREF="javascript:void(0)" onMouseover="alert('對不起,此圖片不能下載!')">
<IMG SRC="2.gif" Align="center" Border="0" width="99" height="50"></A>

頁嵌頁
<iframe width=291 height=247 src="main.files/news.htm" frameBorder=0></iframe>

隱藏滾動條
<body style="overflow-x:hidden;overflow-y:hidden"

CSS文字陰影(定義在<TD>中)
.abc{
FILTER: dropshadow(color=#666666, offx=1, offy=1, positive=1); FONT-FAMILY: "宋體"; FONT-SIZE: 9pt;COLOR: #ffffff;
}

列表/菜單
οnchange="location=this.options[this.selectedIndex].value"

<iframe id="frm" src="k-xinwen.html" scrolling="no" width="314" height="179"></iframe>
<img src="xiangshang.jpg" onMouseOver="sf=setInterval('frm.scrollBy(0,-2)',1)" onMouseOut="clearInterval(sf)" width="31" height="31">
<img src="xiangxia.jpg" onMouseOver="sf=setInterval('frm.scrollBy(0,2)',1)" onMouseOut="clearInterval(sf)" width="31" height="31" >

reurl=server.htmlencode(request.ServerVariables("HTTP_REFERER"))

服務器上如何定義連接
MM_www_STRING ="driver={Microsoft access Driver (*.mdb)};dbq=" & server.mappath("../data/www.mdb")

鏈接到
response.redirect"login.asp"
location.href="xx.asp"

onClick="window.location='login.asp'"
onClick="window.open('')"

取得IP
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")

sql="update feedbak set hit=hit+1 where id="&request("id")
conn.execute(sql)

截取字符是否加...
function formatStr(str,len)
if(len(str)>len)
str = left(str,len) + "..."
end if
formatStr = str
end function

接收表單
If Ucase(Request.ServerVariables("REQUEST_METHOD")) = "POST" then
end if


圖片寬度
<script language="javascript">
<!--
var flag=false;
function DrawImage(ckp){
var image=new Image();
image.src=ckp.src;
if(image.width>0 && image.height>0)
{flag=true;
if(image.width>120){
ckp.width=120;
}else{
ckp.width=image.width;
}
ckp.alt=image.width+"×"+image.height;
}
}
//-->
</script>
I'll be Back 22:18:06
<img src="<%=formPath%>/<%=rs("photoname")%>" border="0" οnlοad="javascript:DrawImage(this);">

跳轉
<meta http-equiv=refresh content='0; url=/distributor/distributor.aspx'>

溢出欄的設制
visible:超出的部分照樣顯示;
hidden:超出的部分隱藏;
scrool:不管有否超出,都顯示滾動條;
auto:有超出時纔出現滾動條;

onMouseOver:鼠標移到目標上;
onMouseUp:按下鼠標再放開左鍵時;
onMouseOut:鼠標移開時;
onMouseDown:按下鼠標時(不需要放開左鍵);
onClink:點擊時;
onDblClick:雙擊時;
onLoad:載入網頁時;
onUnload:離開頁面時;
onResize:當瀏覽者改變瀏覽窗口的大小時;
onScroll:當瀏覽者拖動滾動條的時。

CSS樣式
a:link:表示已經鏈接;
a:hover:表示鼠標移上鍊接時;
a:active:表示鏈接激活時;
a:visited:表示己點擊過的鏈接。

跳出對話框鏈接
javascript:alert('lajflsjpjwg')
後退:javascript:history.back(1)
關閉窗口:javascript:window.close();
窗口還原
function restore(){
window.moveTo(8,8);
window.resizeTo(screen.width-24,screen.availHeight-24);
}

head區是指首頁HTML代碼的<head>和</head>之間的內容。
必須加入的標籤

1.公司版權註釋
<!--- The site is designed by Maketown,Inc 06/2000 --->

2.網頁顯示字符集
簡體中文:<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
繁體中文:<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=BIG5">
英 語:<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

3.網頁製作者信息
<META name="author" content="[email protected] ">

4.網站簡介
<META NAME="DESCRIPTION" CONTENT="xxxxxxxxxxxxxxxxxxxxxxxxxx">

5.搜索關鍵字
<META NAME="keywords" CONTENT="xxxx,xxxx,xxx,xxxxx,xxxx,">

6.網頁的css規範
<LINK href="style/style.css" rel="stylesheet" type="text/css">
(參見目錄及命名規範)

7.網頁標題
<title>xxxxxxxxxxxxxxxxxx</title>

.可以選擇加入的標籤

1.設定網頁的到期時間。一旦網頁過期,必須到服務器上重新調閱。
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">

2.禁止瀏覽器從本地機的緩存中調閱頁面內容。
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

3.用來防止別人在框架裏調用你的頁面。
<META HTTP-EQUIV="Window-target" CONTENT="_top">

4.自動跳轉。
<META HTTP-EQUIV="Refresh" CONTENT="5;URL=http://www.yahoo.com">
5指時間停留5秒。

5.網頁搜索機器人嚮導.用來告訴搜索機器人哪些頁面需要索引,哪些頁面不需要索引。
<META NAME="robots" CONTENT="none">
CONTENT的參數有all,none,index,noindex,follow,nofollow。默認是all。

6.收藏夾圖標
<link rel = "Shortcut Icon" href="favicon.ico">

所有的javascript的調用盡量採取外部調用.
<SCRIPT LANGUAGE="javascript" SRC="script/xxxxx.js"></SCRIPT>

附<body>標籤:
<body>標籤不屬於head區,這裏強調一下,爲了保證瀏覽器的兼容性,必須設置頁面背景<body bgcolor="#FFFFFF">

flash透明
在flash的源代碼中加上:<param name="wmode" value="transparent">
表格透明
style="FILTER: alpha(opacity=72)"

網址前添加icon的方法
1、上http://www.favicon.com 上用他的icon editor online製作一個圖標。他會將做好的圖標通過email即時發送給你。
2、把這個命名爲favicon.ico的圖標放置在index.html同一個文件夾中。就可以了。
作一個圖標文件,大小爲16*16像素。文件擴展名爲ico,然後上傳到相應目錄中。在HTML源文件“<head></head>”之間添加如下代碼:
<Link Rel="SHORTCUT ICON" href="http://圖片的地址(注意與剛纔的目錄對應)">
其中的“SHORTCUT ICON”即爲該圖標的名稱。當然如果用戶使用IE5或以上版本瀏覽時,就更簡單了,只需將圖片上傳到網站根目錄下,自動識別

可以在收藏夾中顯示出你的圖標<link rel="Bookmark" href="favicon.ico">

狀態欄連接說明
<A HREF="鏈接到某處" onmouseOver="window.status='連接說明';return true;" onMouseOut="window.status=' ';">某某鏈接</a>

鏈接說明
<a href=“”Title=鏈接說明>

禁止鼠標右鍵
在<body>標籤中加入 <body οncοntextmenu="return false">

DW裏輸入空格
插入N個&nbsp;

水平線
<hr width="長度" size="高度" color="顏色代碼" noshade> noshade爲有無陰影

表單電子郵件提交
< form name="content" method="post" action="mailto:電子郵箱" >< /form>
文本域名爲Subject 爲郵件的標題

郵件鏈接定製
Mailto:地址 ? Subject=郵件的標題 &bc=抄送 &bcc=密件抄送

背景音樂
<bgsound src=地址 loop="-1">

禁止頁面正文選取
<body οncοntextmenu="return false" οndragstart="return false" onselectstart ="return false" οnselect="document.selection.empty()" οncοpy="document.selection.empty()" onbeforecopy="return false"οnmοuseup="document.selection.empty()">

消除ie6自動出現的圖像工具欄,設置 GALLERYIMG屬性爲false或no .
<IMG SRC="mypicture.jpg" HEIGHT="100px" WIDTH="100px" GALLERYIMG="no">

防止點擊空鏈接時,頁面往往重置到頁首端。
代碼“javascript:void(null)”代替原來的“#”標記

如何避免別人把你的網頁放在框架中
<script language=“javascript”><!--if (self!=top){top.location=self.location;} -->< /script>

頁面定時刷新
<meta http-equiv="Refresh" content="秒" >

頁面定時轉向新的地址
<meta http-equiv="refresh" content="秒;URL=url">

顯示日期
<script language="javascript"><!--
today=new Date();
var week; var date;
if(today.getDay()==0) week="星期日"
if(today.getDay()==1) week="星期一"
if(today.getDay()==2) week="星期二"
if(today.getDay()==3) week="星期三"
if(today.getDay()==4) week="星期四"
if(today.getDay()==5) week="星期五"
if(today.getDay()==6) week="星期六"
date=(today.getYear())+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日"+" "
document.write("<span style='font-size: 9pt;'>"+date+week+"</span>");
// -->
</script>

設爲首頁
<A href=# οnclick="this.style.behavior='url(#default#homepage)';this.setHomePage('url');">設爲首頁</A>

添加收藏
<A href="javascript:window.external.AddFavorite('url','title')">收藏本站</A>

文字滾動
插入邊框爲0的1行1列的表格,在表格中輸入文字,選中文字,
按ctrl+t輸入marquee direction="up", 回車即可讓文字在表格區域內向上滾動。
(right、down可用於讓文字或圖象向右及向下滾動,修改html原代碼還可以得到需要的滾動速度。


表單驗正
<SCRIPT language=javascript>
function checkform(theform){
if(theform.name.value==""){
alert("姓名不能爲空!");
theform.name.focus();
return false;
}
if(theform.tel.value==""){
alert("電話不能爲空!");
theform.tel.focus();
return false;
}
}
</SCRIPT>

定義鼠標
body{cursor: url(cur.ani或cur);}

以圖片方式插視頻
<IMG height=240 loop=infinite dynsrc=http://amedia.efu.com.cn/EFUADD0001.rmvb width=320>

層在flash上面
< param name="wmode" value="opaque" >

延遲跳轉
<meta http-equiv=refresh content='3; url=javascript:window.close();'>

導航條變色:
單元格<TR後面插入οnmοuseοver="javascript:this.bgColor='#57AE00'" οnmοuseοut="javascript:this.bgColor='#99CCFF'"

居中
<CENTER></CENTER>

空鏈接
javascript:;

標題表格
<fieldset>
<legend>表格的說明</legend>
</fieldset>

細線表格
style="BORDER-COLLAPSE: collapse;"

滾動條顏色代碼
BODY{
SCROLLBAR-FACE-COLOR: #FFFFFF;
SCROLLBAR-HIGHLIGHT-COLOR: #FFFFFF;
SCROLLBAR-SHADOW-COLOR: #FFFFFF;
SCROLLBAR-3DLIGHT-COLOR: #FFCBC8;
SCROLLBAR-ARROW-COLOR: #FFFFFF;
SCROLLBAR-TRACK-COLOR: #FFFFFF;
SCROLLBAR-DARKSHADOW-COLOR: #FFCBC8;
SCROLLBAR-BASE-COLOR: #FFFFFF
}

連續的英文或者一堆感嘆號!!!不會自動換行的問題
只要在CSS中定義瞭如下句子,可保網頁不會再被撐開了

table{table-layout: fixed;}
td{word-break: break-all; word-wrap:break-word;}

註釋一下:

1.第一條table{table-layout: fixed;},此樣式可以讓表格中有!!!(感嘆號)之類的字符時自動換行。

2.td{word-break: break-all},一般用這句這OK了,但在有些特殊情況下還是會撐開,因此需要再加上後面一句{word-wrap:break-word;}就可以解決。此樣式可以讓表格中的一些連續的英文單詞自動換行。


1、控制橫向和縱向滾動條的顯隱?
<body style="overflow-y:hidden"> 去掉x軸
<body style="overflow-x:hidden"> 去掉y軸
<body scroll="no">不顯
2、表格變色
<TD οnmοuseοver="this.style.backgroundColor='#FFFFFF'"
οnmοuseοut="this.style.backgroundColor=''"
style="CURSOR: hand">
3、 禁止複製,鼠標拖動選取
<body οndragstart=window.event.returnValue=false οncοntextmenu=window.event.returnValue=false onselectstart=event.returnValue=false>
4、 普通iframe頁面
<iframe name="name" src="main.htm" width="450" height="287" scrolling="Auto" frameborder="0"></iframe>
5、iframe自適應高度
<iframe name="pindex" src="index.asp" frameborder=false scrolling="auto" width="100%" height="100%" frameborder=no οnlοad="document.all['pindex'].style.height=pindex.document.body.scrollHeight" ></iframe>
6、IE地址欄前換成自己的圖標&可以在收藏夾中顯示出你的圖標
<link rel="Shortcut Icon" href="favicon.ico">
<link rel="Bookmark" href="favicon.ico">
7、字號縮放
越來越多的人長時間的泡網,眼鏡的普及率也越來越高,讓文字大點,讓更多的用戶看的更清楚。
<script type="text/javascript">
function doZoom(size)
{document.getElementById('zoom').style.fontSize=size+'px';}
</script>
<span id="zoom">需要指定大小的文字</span>
<a href="javascript:doZoom(16)">大</a> <a href="javascript:doZoom(14)">中</a> <a href="javascript:doZoom(12)">小</a>
8、select擋住div的解決方法
在div里加入下面的代碼,根據需要調整就可以了。
<iframe src="javascript:false" scrolling="no" frameborder="0" style="z-index:-1;position:absolute; top:5px; left:2px;width:168;height:100px;">
</iframe>
9、iframe(嵌入式幀)自適應高度
填寫的嵌入地址一定要和本頁面在同一個站點上,否則會提示“拒絕訪問!”。對跨域引用有權限問題,請查閱其他資料。
<iframe name="guestbook" src="gbook/index.asp" scrolling=no width="100%" height="100%" frameborder=no οnlοad="document.all['guestbook'].style.height=guestbook.document.body.scrollHeight"></iframe>
10、 跳轉菜單新窗口
<select name="select" οnchange="window.open(this.options[this.selectedIndex].value)">
<option value="http://www.microsoft.com/ie "> Internet Explorer</option>
<option value="http://www.microsoft.com "> Microsoft Home</option>
<option value="http://msdn.microsoft.com "> Developer Network</option>
</select>
11、flash透明選項
<param name="wmode" value="transparent">
12、添加到收藏夾和設爲首頁
<a href=# οnclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.makewing.com/lanren/');">設爲首頁</a>

<a href="javascript:window.external.AddFavorite('http://www.makewing.com/lanren/','懶人圖庫')">收藏本站</a>
13、記錄並顯示網頁的最後修改時間
<script language=JavaScript>
document.write("最後更新時間: " + document.lastModified + "")
</script>
14、節日倒計時
<Script Language="JavaScript">
   var timedate= new Date("October 1,2002");
   var times= "國慶節";
   var now = new Date();
   var date = timedate.getTime() - now.getTime();
   var time = Math.floor(date / (1000 * 60 * 60 * 24));
   if (time >= 0)
   document.write( "現在離"+times+"還有: "+time +"天")
</Script>
15、加在HEAD裏
禁止緩存
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
16、 讓IFRAME框架內的文檔的背景透明
<iframe src="about:<body style='background:transparent'>" allowtransparency></iframe>

發佈了60 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章