javascript常见代码

  javascript常见代码[2002年8月27日]
<title>javascript常用代码收藏</title>
<style>
p {font-size:12px;margin:3px;text-indent:24px;font-family:verdana}
textarea {width:750;height:80;}
input {width:80;height:20}
.title {font-weight:bold;text-indent:0px;background:#eeeeee;padding:5px}
</style>
<script>
function runCode(obj){
var textsss=eval('document.all.'+obj).value;
var runWin=window.open();
runWin.document.write(textsss);
runWin.document.close();
}
</script>
<center>
<table width=760><tr><td>
<p class=title>1. 最小化、最大化、关闭窗口</p>
<textarea id=a1>
<OBJECT id="min" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM name="Command" value="Minimize"></OBJECT>
<button οnclick="min.Click()"><font face="webdings">0</font></button>
<OBJECT id="max" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM name="Command" value="Maximize"></OBJECT>
<button οnclick="max.Click()"><font face="webdings">1</font></button>
<OBJECT id="clo" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM name="Command" value="Close"></OBJECT>
<button οnclick="clo.Click()"><font face="webdings">r</font></button>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a1')">
<p class=title>2. 新打开的窗口在屏幕上居中</p>
<p>window.open()方法</p>
<textarea id=a2>
<script>
window.open("about:blank","new","height=80,width=300,left="+(screen.width/2-150)+",top="+(screen.height/2-40)+",toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no")
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a2')">
<p>showModelessDialog()方法</p>
<textarea id=a3>
<script>
showModelessDialog("new.htm","","dialogHeight:180px;dialogWidth:300px;center:yes;help:no");
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a3')">
<p class=title>3. 怎样使回车键具有Tab键的功能?</p>
<textarea id=a4>
<script>
function change() {
if (event.keyCode==13) {
event.keyCode=9;
}
}
</script>
<body οnkeydοwn="change()">
<input type="text" value="回车试试!">
<input type="text" value="回车试试!">
<input type="text" value="回车试试!">
<input type="text" value="回车试试!">
</textarea><br><input type=button value=运行代码 οnclick="runCode('a4')">
<p class=title>4. 如何禁止自己的页面在别人的框架里打开?</p>
<textarea id=a5>
<script>
if (window.top!=self){
window.top.location=self.location
}
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a5')">
<p class=title>5. 在当前光标处插入文字</p>
<textarea id=a6>
<script>
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
}
else
textEl.value = text;
}
function deleteAtCaret (textEl) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
document.selection.clear();
}
//alert(aForm.aTextArea.
}
</script>
<form name=aForm>
<input name=aTextArea size=120 wrap=soft οnselect="storeCaret(this);" οnclick="storeCaret(this);" οnkeyup="storeCaret(this);" value=这是例子,你可以在这里添加文字、插入文字。>
<br>
<input type=text name=aText size=80 value=我要在光标处插入这些文字><br>
<input type=button value=我要在光标处插入上面文本框里输入的文字! οnclick="insertAtCaret(this.form.aTextArea, this.form.aText.value);">
<input type=button value=删除选中文字 οnclick=deleteAtCaret(this.form.aTextArea); id=button1 name=button1>
</FORM>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a6')">
<p class=title>6. 查看驱动器</p>
<p>使用FSO(FileSystemObject)实现,需要拷贝到本地执行</p>
<textarea id=a7>
<script>
var fso, s, n, e, x, t; // 声明变量。
fso = new ActiveXObject("Scripting.FileSystemObject");
e = new Enumerator(fso.Drives); // 在驱动器上创建 Enumerator。
s = "你的系统磁盘如下:/n";
for (;!e.atEnd();e.moveNext()) // 枚举驱动器集合。
{
x = e.item();
s = s + x.DriveLetter;
s += " - ";
if (x.DriveType == 3) // 查看是否网络驱动。
n = x.ShareName; // 获取共享名。
else if (x.IsReady) // 查看驱动器是否已就绪。
n = x.VolumeName; // 获取卷名。
else
n = "[驱动器未就绪]";
s += n + "/n";
if(x.DriveType==4)
s +="(光驱)" + "/n";
}
alert(s);
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a7')">
<p class=title>7. 打开子窗口的同时关闭父窗口</p>
<textarea id=a8>
<script>
<!--
function openmain(){
closes.Click();
aa=window.open("about:子窗口");
}
//-->
</script>
父窗口
<input type=button value=打开子窗口 οnclick="openmain()">
<OBJECT id=closes type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<param name="Command" value="Close"></OBJECT>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a8')">
<p class=title>8. 用javascript代码实现内容的复制</p>
<textarea id=a9>
<SCRIPT>
function CopytheText(obj)
{
obj.focus() //使文本框得到焦点
obj.select() //把文本框中的内容全选
if (document.all){
therange=obj.createTextRange()
therange.execCommand("Copy") //复制
}
}
</SCRIPT>
<input type=button value=复制 οnclick="CopytheText(text1)"><br>
<input name="text1" size=120 value=你按一下上面的“复制”按钮,就可把这个文本框中的内容复制到剪贴板内了!><br>
<input size=120 value=粘贴到这里>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a9')">
<p class=title>9. 正则表达式应用——验证Email</p>
<p>不包括中文</p>
<textarea id=a10>
<script>
function checkEmail(){
var emailPattern = /^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/;
if (emailPattern.test(emails.value)==false)
alert("非法的Email地址!")
else
alert("正确的Email地址!")
}
</script>
<input id=emails><input type=button value=CheckEmail οnclick=checkEmail()>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a10')">
<p class=title>10. 正则表达式应用——验证数字函数</p>
<textarea id=a11>
<script>
//检查是否为任意数(实数)
function isNumeric(strNumber) {
var newPar=/^(-|/+)?/d+(/./d+)?$/
alert(newPar.test(strNumber)); }
//检查是否为正数
function isUnsignedNumeric(strNumber) {
var newPar=/^/d+(/./d+)?$/
alert(newPar.test(strNumber)); }
//检查是否为整数
function isInteger(strInteger) {
var newPar=/^(-|/+)?/d+$/
alert(newPar.test(strInteger)); }
//检查是否为正整数
function isUnsignedInteger(strInteger) {
var newPar=/^/d+$/
alert(newPar.test(strInteger)); }
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a11')">
<p class=title>11. 正则表达式应用——去掉字符串前后空格</p>
<textarea id=a12>
<script>
function jsTrim(){
var str=strText.value
str = str.replace(/^/s*/g,"")
str = str.replace(//s*$/g,"")
strText.value=str
}
</script>
<input id=strText value=" abd "><input type=button value=去掉字符串前后空格 οnclick=jsTrim()>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a12')">
<p class=title>12. 正则表达式应用——准确匹配一个html标记</p>
<p>微软Script5.6文档上的例子/<(.*)>.*<///1>/匹配一个html标记有误</p>
<textarea id=a13>
<script>
alert(/<([a-z]+) (.*)>.*<///1>/i.test("<a href=sfd>afd</a>"))
alert(/<(/w+?) .+?>.+?<///1>/.test("<a 12>xxx</a>"))
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a13')">
<p class=title>13. 如何用javascript检测Flash插件是否安装?</p>
<textarea id=a14>
<script>
var swf = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
(swf)?document.write('你已经安装了插件'):document.write('你没有安装插件');
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a14')">
<p class=title>14. 如何用javascript检测Flash插件是否安装?</p>
<textarea id=a15>
<object οnerrοr=alert("没装RealPlayer") CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFB白痴CFA"></object>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a15')">
<p class=title>15. javascript 可以读取服务器时间吗?</p>
<p>单纯客户端脚本不能实现</p>
<textarea id=a16>
<script language="javascript" runat=server>
var a =new Date()
Response.write(a.getYear() + "" + (a.getMonth()+1))
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a16')">
<p class=title>16. 获得按键的ASCII码</p>
<textarea id=a17>
<body οnkeydοwn=alert(event.keyCode)>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a17')">
<p class=title>17. 获得字符串的真实长度(Yang)</p>
<p>一般情况下使用objElement.value.length获得的长度里中文是被认为长度为1,这个代码可以获得字符串的真实长度,中文和全角字符都会被认为是两个字节</p>
<textarea id=a18>
<script>
function checkstr(str)
{
num=str.length
var arr=str.match(/[^/x00-/x80]/ig)
if(arr!=null)num+=arr.length
alert(num)
}
</script>
<input id="msg"><input type=button οnclick=checkstr(msg.value) value="检查">
</textarea><br><input type=button value=运行代码 οnclick="runCode('a18')">
<p class=title>18. 不允许后退的代码</p>
<textarea id=a19>
<a href="javascript:location.replace('http://51js.1000box.net/')">51js</a>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a19')">
<p class=title>19. 如何实现四舍五入?</p>
<textarea id=a20>
<script>
var num=12.3456
//方法1:
alert(num+"保留小数点后两位是:"+(num).toFixed(2)) //IE5.5+自带
//方法2(秋水无恨):
function Number.prototype.Fixed(n){
with(Math){var tmp=pow(10,n);return round(this*tmp)/tmp;}
}
alert(num+"保留小数点后两位是:"+num.Fixed(2));
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a20')">
<p class=title>20. 如何用文字链接实现表单的提交和重置功能?</p>
<textarea id=a21>
<form name=formName action="?.asp">
<a href="#" οnclick="formName.submit()">Submit</a>
<a href="#" οnclick="formName.reset()">Reset</a>
</form>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a21')">
<p class=title>21. 动态增加与删除SELECT的Option</p>
<textarea id=a22>
<script>
// 添加选项
function addOption(pos){
var objSelect = document.myForm.mySelect;
// 取得字段值
var strName = document.myForm.myOptionName.value;
var strvalue = document.myForm.myOptionvalue.value;
// 建立Option对象
var objOption = new Option(strName,strvalue);
if (pos == -1 & pos > objSelect.options.length)
objSelect.options[objSelect.options.length] = objOption;
else
objSelect.add(objOption, pos);
}
// 删除选项
function deleteOption(type){
var objSelect = document.myForm.mySelect;
if(objSelect.options.length>0){
if (type == true)
objSelect.options[objSelect.selectedIndex] = null;
else
objSelect.remove(objSelect.selectedIndex);
}
else
alert("已经没有选项可以删除了!")
}
// 显示选项信息
function showOption(objForm){
var objSelect = objForm.mySelect;
if(objSelect.options.length>0){
document.all.myOptionName.value = objSelect.options[objSelect.selectedIndex].text;
document.all.myOptionvalue.value = objSelect.options[objSelect.selectedIndex].value;
}
else
alert("没有选项可以显示!")
}
</script>
<form name="myForm">
<select name="mySelect">
<option value="value1" Selected>HTML</option>
<option value="value2">javascript</option>
<option value="value3">VBScript</option>
</select>
<input type="button" οnclick="showOption(this.form)" value="显示">
<input type="button" οnclick="deleteOption(true)" value="删除">
<input type="button" οnclick="deleteOption(false)" value="Remove方法"><br><br>
选项名称 : <input type="text" name="myOptionName" value="CSS"><br>
选项的值 : <input type="text" name="myOptionvalue" value="value4">
<input type="button" οnclick="addOption(-1)" value="添加">
<input type="button" οnclick="addOption(0)" value="插入">
</form>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a22')">
<p class=title>22. 获得浏览器和系统资源的信息——Navigator对象属性</p>
<textarea id=a23>
<script>
var nav=window.navigator;
for (obj in nav)
document.write(obj+" = "+eval("window.navigator."+obj)+"<br>")
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a23')">
<p class=title>23. 获得屏幕分辨率</p>
<textarea id=a24>
<script>
alert("screen.width = "+screen.width+"/rscreen.height = "+screen.height)
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a24')">
<p class=title>24. 如何让Textarea具有文本框(input type=text)一样的maxlength?(色迷迷的小疯狗,经典论坛)</p>
<p>请先去掉< /textarea>中的空格再运行代码</p>
<textarea id=a25>
<textarea rows="4" cols="30" maxlength=20 id=test οnkeypress=doKeypress() οnkeydοwn=doKeydown() onbeforepaste=doBeforePaste() οnpaste=doPaste()>< /textarea>
<script language=javascript>
element=document.all["test"]
maxLength = element.maxlength
function doKeypress(){
if(!isNaN(maxLength))
{
maxLength = parseInt(maxLength)
var oTR = element.document.selection.createRange()
if(oTR.text.length >= 1)
event.returnvalue = true
else if(element.value.length > maxLength-1)
event.returnvalue = false
}
}
function doKeydown(){
setTimeout(function(){maxLength = parseInt(maxLength);if(!isNaN(maxLength)){if(element.value.length > maxLength-1){var oTR = window.document.selection.createRange();oTR.moveStart("character", -1*(element.value.length-maxLength));oTR.text = ""}}},1)
}
function doBeforePaste(){
if(!isNaN(maxLength))
event.returnvalue = false
}
function doPaste(){
if(!isNaN(maxLength))
{
event.returnvalue = false
maxLength = parseInt(maxLength)
var oTR = element.document.selection.createRange()
var iInsertLength = maxLength - element.value.length + oTR.text.length
var sData = window.clipboardData.getData("Text").substr(0, iInsertLength)
oTR.text = sData;
}
}
</script>
</textarea><br><input type=button value=运行代码 οnclick="runCode('a25')">
<p class=title>25. 点击一个按钮,刷新当前页</p>
<textarea id=a26>
<input type=button value=刷新 οnclick="history.go(0)">
<input type=button value=刷新 οnclick="location.reload()">
<input type=button value=刷新 οnclick="location=location">
<input type=button value=刷新 οnclick="location.assign(location)">
<input type=button value=刷新 οnclick="document.execCommand('Refresh')">
<input type=button value=刷新 οnclick="window.navigate(location)">
<input type=button value=刷新 οnclick="location.replace(location)">
<input type=button value=刷新 οnclick="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=刷新 οnclick="a1.click()">
</textarea><br><input type=button value=运行代码 οnclick="runCode('a26')">
</td></tr></table>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值