JS代码大全

在模式窗口中打开连接不开新窗口

* 在模式窗口中打开连接不开新窗口

在<head></head>中加入加入以下代码:

<head></head>中加入<basetarget="_self">

常用JavaScript語句

1.document.write(""); 输出语句

2.js中的注释为//

3.传统的html文档顺序是:document->html->(head,body)

4.一个浏览器窗口中的dom顺序是:window->(navigator,screen,history,location,document)

5.得到表单中元素的名称和值:document.getelementbyid("表单中元素的id號").name(或value)

6.一个小写转大写的js:document.getelementbyid("output").value =document.getelementbyid("input").value.touppercase();

7.js中的值类型:string,number,boolean,null,object,function

8.js中的字符型转换成数值型:parseint(),parsefloat()

9.js中的数字转换成字符型:(""变量)

10.js中的取字符串长度是:(length)

13.js中声明变量使用:var来进行声明

18.当文件中出现多个form表单时.可以用document.forms[0],document.forms[1]来代替.

19.窗口:打开窗口window.open(),关闭一个窗口:window.close(), 窗口本身:self

20.状态栏的设置:window.status="字符";

22.弹出確认框:window.confirm();

23.弹出输入提示框:window.prompt();

24.指定当前显示链接的位置:window.location.href="url"

25.取出窗体中的所有表单的数量:document.forms.length

26.关闭文档的输出流:document.close();

28.创建一个文档元素:document.createelement(),document.createtextnode()

29.得到元素的方法:document.getelementbyid()

33.单选按钮组判断是否被选中也是用checked

35.字符串的定义:var mystring =new string("this is lightsword");

36.字符串转成大写:string.touppercase();字符串转成小写:string.tolowercase();

37.返回字符串2在字符串1中出现的位置:string1.indexof("string2")!=-1则说明没找到.

38.取字符串中指定位置的一个字符:stringa.charat(9);

39.取出字符串中指定起点和终点的子字符串:stringa.substring(2,6);

40.数学函数:math.pi(返回圆周率),math.sqrt2(返回开方),math.max(value1,value2)返回两个数中的最在值,math.pow(value1,10)返回value1的十次方,math.round(value1)四舍五入函数,math.floor(math.random()*(n1))返回隨机数

41.定义日期型变量:var today =new date();

42.日期函数列表:dateobj.gettime()得到时间

dateobj.getyear()得到年份

dateobj.getfullyear()得到四位的年份

dateobj.getmonth()得到月份

dateobj.getdate()得到日

dateobj.getday()得到日期几

dateobj.gethours()得到小时

dateobj.getminutes()得到分

dateobj.getseconds()得到秒

dateobj.settime(value)设置时间dateobj.setyear(val)设置年,dateobj.setmonth(val)设置月,dateobj.setdate(val)设置日,dateobj.setday(val)设置星期几,dateobj.sethours设置小时,dateobj.setminutes(val)设置分,dateobj.setseconds(val)设置秒 [注意:此日期时间从0开始计]<br/>

44.parent代表父亲对象,top代表最顶端对象

45.打开子窗口的父窗口为:opener

46.表示当前所属的位置:this

50.指定在不支持脚本的浏览器显示的html:<noscript></noscript>

51.当超链和onclick事件都有时,则老版本的浏览器转向a.html,否则转向b.html.例:<ahref="a.html" onclick="location.href=''b.html'';returnfalse">dfsadf</a>

52.js的內建对象有:array,boolean,date,error,evalerror,function,math,number,object,rangeerror,referenceerror,regexp,string,syntaxerror,typeerror,urierror

53.js中的换行:\n

54.窗口全屏大小:<script>

function fullscreen(){

this.moveto(0,0);

this.outerwidth=screen.availwidth;this.outerheight=screen.availheight;}

window.maximize=fullscreen;</script>

55.js中的all代表其下层的全部元素

56.js中的焦点顺序:document.getelementbyid("表单元素").tabindex= 1

57.innerhtml的值是表单元素的值:如<pid="para">"how are <em>you</em>"</p>,则innerhtml的值就是:how are<em>you</em>

58.innertext的值和上面的一样,只不过不会把<em>这种標记显示出来.

59.contenteditable可设置元素是否可被修改,iscontenteditable返回是否可修改的状态.

60.isdisabled判断是否为禁止状态.disabled设置禁止状态

61.length取得长度,返回整型数值

62.addbehavior()是一种js调用的外部函数文件其扩展名为.htc

63.window.focus()使当前的窗口在所有窗口之前.

64.blur()指失去焦点.与focus()相反.

65.select()指元素为选中状态.

66.防止用户对文本框中输入文本:onfocus="this.blur()"

69.状态栏文字的设置:window.status=''文字'',默认的状态栏文字设置:window.defaultstatus= ''文字.'';<br />

71.js中遇到脚本错误时不做任何操作:window.onerror= donothing; 指定错误句柄的语法为:window.onerror = handleerror;

72.js中指定当前打开窗口的父窗口:window.opener,支持opener.opener...的多重继续.

76.js中关闭当前的窗口:window.close();

77.js中提出是否確认的框:if(confirm("areyou sure?")){alert("ok");}else{alert("not ok");}

78.js中的窗口重定向:window.navigate("http://www.sina.com.cn");

79.js中的打印:window.print()

80.js中的提示输入框:window.prompt("message","defaultreply");

81.js中的窗口滚动条:window.scroll(x,y)

82.js中的窗口滚动到位置:window.scrollby

83.js中设置时间间隔:setinterval("expr",msecdelay)或setinterval(funcref,msecdelay)或settimeout

84.js中的模态显示在ie4 行,在nn中不行:showmodaldialog("url"[,arguments][,features]);

85.js中的退出之前使用的句柄:functionverifyclose(){event.returnvalue="we really like you and hope you will staylonger.";}} window.=verifyclose;

86.当窗体第一次调用时使用的文件句柄:onload()

87.当窗体关闭时调用的文件句柄:onunload()

88.window.location的属性:protocol(http:),hostname(<a href="http://www.example.com),port(80),host(www.example.com:80),pathname("/a/a.html"),hash("#giantgizmo">www.example.com),port(80),host(www.example.com:80),pathname("/a/a.html"),hash("#giantgizmo</a>",指跳转到相应的锚记),href(全部的信息)

89.window.location.reload()刷新当前页面.

90.window.history.back()返回上一页,window.history.forward()返回下一页,window.history.go(返回第几页,也可以使用访问过的url)

91.document.write()不换行的输出,document.writeln()换行输出

93.变量名.charat(第几位),取该变量的第几位的字符.

94."abc".charcodeat(第几个),返回第几个字符的ascii码值.

95.字符串连接:string.concat(string2),或用 =进行连接

96.变量.indexof("字符",起始位置),返回第一个出现的位置(从0开始计算)

97.string.lastindexof(searchstring[,startindex])最后一次出现的位置.

98.string.match(regexpression),判断字符是否匹配.

99.string.replace(regexpression,replacestring)替换现有字符串.

100.string.split(分隔符)返回一个数组存储值.

101.string.substr(start[,length])取从第几位到指定长度的字符串.

102.string.tolowercase()使字符串全部变为小写.

103.string.touppercase()使全部字符变为大写.

104.parseint(string[,radix(代表进制)])强制转换成整型.

105.parsefloat(string[,radix])强制转换成浮点型.

106.isnan(变量):测试是否为数值型.

107.定义常量的关键字:const,定义变量的关键字:var

javascirpt网页常用代码集合

1、oncontextmenu="window.event.returnvalue=false"将彻底屏蔽鼠标右键

2、<bodyonselectstart="return false"> 取消选取、防止复制

3、onpaste="returnfalse" 不准粘贴

4、oncopy="returnfalse;" oncut="return false;" 防止复制

5、<linkrel="shortcut icon" href="favicon.ico"> ie地址栏前换成自己的图标

6、<linkrel="bookmark" href="favicon.ico"> 可以在收藏夹中显示出你的图标

7、<inputstyle="ime-mode:disabled"> 关闭输入法

8、永远都会带着框架

<scriptlanguage="javascript">

<!--

if (window == top)top.location.href ="frames.htm"; /frames.htm为框架网页

/ -->

</script>

9、防止被人frame

<script language=javascript>

<!--

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

/ -->

</script>

10、网页将不能被另存为

<noscript><iframesrc=*.html></iframe></noscript>

11、<inputtype=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http:/www.webjx.com/'">

12、删除时确认

<a href='javascript:if(confirm("确实要删除吗?"))location="boos.asp?&areyou=删除&page=1"'>删除</a>

13、取得控件的绝对位置

//javascript

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

原文地址:https://www.cnblogs.com/mattins/p/1848090.html