JavaScript常用代碼段

nef5 9年前發布 | 6K 次閱讀 JavaScript

把如下代碼加入<body>區域中:    

后退 前進    

<!--<input type="button" value="后退" onClick="history.go(-1)">    

<input type="button" value="前進" onClick="history.go( 1 );return true;">    

 

返回    

<form><input type="button" value="返回上一步" onClick="history.back(-1)"></form>    

 

查看源碼    

<input type="button" name="view" value="查看源碼" onClick="window.location="view-source:" +window.location.href">    

 

禁止查看源碼    

<body oncontextmenu="return false"></body>    

 

刷新按鈕一    

<input type="button" value="刷新按鈕一" onClick="ReloadButton()">    

<script>function ReloadButton(){location.href="i001.htm";}</script>    

 

刷新按鈕二    

<input type="button" value="刷新按鈕二" onClick="history.go(0)">    

 

回首頁按鈕    

<input type="button" value="首頁" onClick="HomeButton()">    

<script>function HomeButton(){location.href=http://www.winliuxq.com/;}</script>    

 

彈出警告框    

<input type="button" value="彈出警告框" onClick="AlertButton()">    

<script>function AlertButton(){window.alert("要多多光臨呀!");}</script>    

 

狀態欄信息    

<input type="button" value="狀態欄信息" onClick="StatusButton()">    

<script>function StatusButton(){window.status="要多多光臨呀!";}</script>    

 

背景色變換    

<form><input type="button" value="背景色變換" onClick="BgButton()"></form>    

<script>function BgButton(){    

if (document.bgColor=='#00ffff')    

     {document.bgColor='#ffffff';}    

else{document.bgColor='#00ffff';}    

}    

</script>    

 

打開新窗口    

<input type="button" value="打開新窗口" onClick="NewWindow()">    

<script>function NewWindow(){window.open("c01.htm","","height=240,width=340,status=no,location=no,toolbar=no,directories=no,menubar=no");}    

</script>    

 

窗口最小化    

<OBJECT id="min" type="application/x- oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">< PARAM name="Command" value="Minimize"></OBJECT>< button onClick="min.Click()">窗口最小化</button>    

 

全屏代碼    

<input type="BUTTON" name="FullScreen" value="全屏顯示" onClick="window.open(document.location, 'butong_net', 'fullscreen')">    

 

關閉窗口    

<OBJECT id=closes type="application/x- oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">< param name="Command" value="Close"></object>< input type="button" value="關閉窗口" onClick="closes.Click();">    

 

關閉窗口    

<input type=button value=關閉窗口 onClick="javascript:self.close()">    

 

最大化    

<object id=big classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">    

<param name="Command" value="Maximize"></object><input type=button value=最大化 onClick=big.Click()>    

 

關閉輸入法    

<input style="ime-mode:disabled" value=關閉輸入法>    

 

鏈接按鈕1    

<input type="button" value="鏈接按鈕 1" onClick="window.open('http://www.winliuxq.com/', 'Sample', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=yes,width=790,height=520,left=0,top=0')" name="input">    

 

鏈接按鈕2    

<input type="BUTTON" NAME="Button" value="鏈接按鈕2" onClick="showModalDialog('http://www.winliuxq.com/')">    

 

鏈接按鈕3    

<input type="submit" value="鏈接按鈕3" onClick="location.">    

 

警告框顯示源代碼    

<BUTTON onClick=alert(document.documentElement.outerHTML) style="width:110">警告框顯示源代碼</BUTTON>    

 

點擊后按鈕清空    

<input type=button value='打印' onClick="this.style.visibility='hidden';window.print();">    

 

打印    

<input type=button value='打印' onClick="window.print();">    

 

打印預覽    

<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>    

<input type=button value=打印預覽 onclick="wb.execwb(7,1)">    

 

另存為    

<input onClick="document.execCommand('saveas','true','常用代碼.htm')" type=button value=另存為>    

 

點擊自動復制    

<script>function oCopy(obj){obj.select();js=obj.createTextRange();js.execCommand("Copy")}</script>    

<input type="text" value="點擊自動復制" onClick="oCopy(this)" size="11">    

 

自動選中    

<input value="自動選中" onFocus="this.select()" onMouseOver="this.focus()" size="11">    

 

打開源代碼    

<BUTTON onClick="document.location = 'view-source:' + document.location" size="7">打開源代碼</BUTTON>    

 

新窗口延遲打開    

<input type=button value=新窗口延遲打開 onClick=javascript:setTimeout("window.open('http://www.winliuxq.com/')",10000)>    

 

實現選中文本框里的前一半的內容    

<input type="text" value="選中文本框里的前一半的內容" size=30 onmouseover="this.select();tR=document.selection.createRange();tR.moveEnd('character',-8);tR.select();">    

<input type="text" value="選中部分內容,非IE可以用這個" size=30 onmouseover="this.selectionStart=this.value.length-4;this.selectionEnd=this.value.length">    

 

點擊清空文字    

<input type="text" name="artist" size=14 value="點擊清空文字" onmouseover=this.focus() onfocus=this.select() onclick="if(this.value==' 點擊清空文字')this.value=''">    

 

點擊清空文字    

<input name=name size=11 value=點擊清空文字 onMouseOver=this.focus() onblur="if (this.value =='') this.value='點擊清空文字'" onFocus=this.select() onClick="if (this.value=='點擊清空文字 ') this.value=''">    

 

等于標題(title):    

<input type="text" value="" id="aa" size="20">    

<script>document.getElementById("aa").value=document.title;</script>    

 

檢測IE是否脫機    

<input type="button" value="測試" onclick="alert(window.navigator.onLine)">    

 

11種刷新按鈕的方法    

<input type=button value=刷新 onClick="history.go(0)">    

<input type=button value=刷新 onClick="location.reload()">    

<input type=button value=刷新 onClick="location=location">    

<input type=button value=刷新 onClick="location.assign(location)">    

<input type=button value=刷新 onClick="document.execCommand('Refresh')">    

<input type=button value=刷新 onClick="window.navigate(location)">    

<input type=button value=刷新 onClick="location.replace(location)">    

<input type=button value=刷新 onClick="window.open('自身的文件','_self')">    

<input type=button value=刷新 onClick=document.all.WebBrowser.ExecWB(22,1)>    

<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>    

<form action="自身的文件"><input type=submit value=刷新></form>    

<a id=a1 href="自身的文件"></a><input type=button value=刷新 onClick="a1.click()"> 

 

========================================================================================================= 

腳本說明:    

把如下代碼加入<body>區域中:    

<a href="#" onClick=document.execCommand("open")>打開</a>   

 

<a onclick="window.open('i001.htm','','height=300,width=300,resizable=no,location=net');" href="">打開指定大小網頁</a   

 

<a href="#" onClick=location.replace("view-source:"+location)>使用 記事本 編輯</a>   

 

<a href="#" onClick=document.execCommand("saveAs")>另存為</a>   

 

<a href="#" onClick=document.execCommand("print")>打印</a>   

<a href="javascript:window.print();">打印</a>   

 

<a href=mailto:429752806@163.com>發送E-mail</a>   

 

<a href="#" onClick=document.execCommand("selectAll")>全選</a>   

 

<a href="#" onClick=location.reload()>刷新1</a>   

 

<a href="#" onClick=history.go(0)>刷新2</a>   

 

<a href="#" onClick=location.replace("view-source:"+location)>查看源文件</a>   

 

<a href="#" onClick=window.open(document.location,"url","fullscreen")>全屏顯示</a>   

 

<a href="#" onClick=window.external.showBrowserUI("PrivacySettings",null)>internet選項</a>   

 

<a href="#" oncontextmenu="window.open(this.href);return false;">單擊右鍵將在新窗口中打開</a>   

 

<a href="#" onClick=history.go(1)>前進1</a>   

 

<a href="#" onClick=history.forward()>前進2</a>   

 

<a href="#" onClick=history.go(-1)>后退1</a>   

 

<a href="#" onClick=history.back()>后退2</a>   

 

<a href="#" onClick=window.external.showBrowserUI("OrganizeFavorites",null)>整理收藏夾</a>   

 

<SPAN onClick="window.external.addFavorite('http://www.winliuxq.com /','網頁特效站點')" style="CURSOR: hand" title=網頁特效站點>加入收藏</SPAN>    

 

<a href="#" onClick="window.external.addFavorite('http://www.winliuxq.com/','網頁特效站點')">添加到收藏夾</A>   

 

<a href="javascript:window.external.AddFavorite('http://www.winliuxq.com/', '網頁特效站點')">點擊加入收藏夾</a>   

 

<a href="#" onmouseover="window.external.addFavorite('http://www.winliuxq.com /','網頁特效站點')" target="_self" >鼠標感應收藏本站</a>   

 

<a href="#" onmouseover="this.style.behavior='url(#default#homepage)'; this.setHomePage('http://www.yiloo.com/js/');" target="_self">鼠標感應設為首頁</a>    

 

<a href="javascript:window.close()">關閉窗口</a>   

 

<a href="#" onClick=window.close();return false)>關閉窗口</a>   

 

<a href="#" onClick=setTimeout(window.close(),3000)>3秒關閉本窗口</a>   

 

<script>   

function shutwin(){    

window.close();    

return;}    

</script>   

<a href="javascript:shutwin();">關閉本窗口</a>   

 

<SPAN onClick="var strHref=window.location.href; this.style.behavior='url(#default#homepage)'; this.setHomePage('http://www.winliuxq.com/');" style="CURSOR: hand">設為首頁</SPAN>   

 

等于標題欄:<script>document.write(document.title);</script>   

 

<a href="javascript:void(0);" onClick='window.external.AddFavorite(location.href, document.title);'>收藏本頁</a>   

 

<a href="javascript:window.external.AddFavorite(document.location.href, document.title)">收藏本頁</a>   

 

<a href=javascript:window.external.addChannel("typhoon.cdf")>加入頻道</a>   

 

<a href="i003.htm" onclick="return false;" ondblclick="window.open('i003.htm');">雙擊打開鏈接</a>   

 

<style>#close a:hover {background:url(javascript:window.opener=0; window.close());}</style><div id=close><a href="">關閉窗口</a></div>   

 

<A HREF="javascript:void(0)" onMouseover="alert('對不起,禁止選中!')">鏈接禁止</A>   

<a href="" onMouseOver="alert('本站域名:http://www.winliuxq.com/');return true;">記住本站域名</a>   

 

滾動條在左側,將<html>改為<HTML DIR="RTL"> 

==================================================================================================== 

網頁半透明    

<body style="filter:Alpha(Opacity=50)">   

 

隨機選擇背景色    

<body>   

<script>   

document.body.style.background=(["red","blue","pink","navy","gray","yellow","green","purple"])[parseInt(Math.random()*8)];    

</script>   

 

框架頁中不顯示滾動條:    

<SCRIPT>   

self.moveTo(0,0)    

self.resizeTo(screen.availWidth,screen.availHeight)    

</SCRIPT>   

 

防止網頁被框架    

<SCRIPT LANGUAGE=JAVASCRIPT>   

if (top.location !== self.location) {    

top.location=self.location;    

}    

</SCRIPT>   

 

永遠都會帶著框架    

<script language="javascript"><!--    

if (window == top)top.location.href = "frame.htm"; //frame.htm為框架網頁    

// --></script>   

 

窗口自動最大化    

<script language="JavaScript"><!--    

self.moveTo(0,0)    

self.resizeTo(screen.availWidth,screen.availHeight)    

//--></script>   

 

打開窗口自動最大化    

<OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT><PARAM name="Command" value="Maximize"></OBJECT>   

 

爽眼閃屏代碼    

<script>var color = new Array;color[1] = "black";color[2] = "white";for(x = 0; x <3; x++){document.bgColor = color[x];if(x == 2){x = 0;}}</script>   

 

不能被另存為    

<noscript><iframe src="*.html"></iframe></noscript>   

 

漢字字庫調用    

<script>   

for(i=19968;i<40870;i++)document.write(String.fromCharCode(i));    

</script>   

 

顯示現在時間的腳本    

<script language=vbscript>document.write now</script>   

 

顯示最后修改時間的腳本    

<script>document.write(document.lastModified)</script>    

 

按下F12鍵,直接返回首頁    

<script>function look(){    

if(event.keyCode==123){document.location.href=http://www.winliuxq.com/}    

}    

if(document.onkeydown==null)    

{document.onkeydown=look}    

</script>    

 

端口檢測    

<img src="http://www.winliuxq/zza.jpg" onload="alert('端口可用')" onerror="alert('端口禁止')"...>   

 

無法最小化的窗口    

<body onblur='self.focus();'>   

 

鏈接點外部css文件    

<style>@import url("ie.css");</style>   

 

內嵌式框架-網頁中調用另外網頁:    

<object type="text/x-scriptlet" width="600"   height="1000" data="http://www.winliuxq.com/"></object>   

 

刷新改變窗口大小    

<OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT><PARAM name="Command" value="Maximize"></OBJECT>   

 

JavaScript實現網頁豎虛線    

<script>hei=120;d1=2;d2=2;cou=Math.floor(hei/(d1+d2));    

document.write('<table cellspacing=0 cellpadding=0 width=1 height='+hei+'>');    

for(i=0;i<cou;i++){document.write('<tr><td height='+d2+'><tr><td height='+d1+' bgcolor=333333>')}</script></table>

 

js翻頁    

<script>document.write("<a href="+location.href.replace(/\.html/g,"_2.html")+">2</a>");</script>

 本文由用戶 nef5 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!