JavaScript常用内置函数.docx

上传人:小飞机 文档编号:3159446 上传时间:2023-03-11 格式:DOCX 页数:11 大小:40.60KB
返回 下载 相关 举报
JavaScript常用内置函数.docx_第1页
第1页 / 共11页
JavaScript常用内置函数.docx_第2页
第2页 / 共11页
JavaScript常用内置函数.docx_第3页
第3页 / 共11页
JavaScript常用内置函数.docx_第4页
第4页 / 共11页
JavaScript常用内置函数.docx_第5页
第5页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《JavaScript常用内置函数.docx》由会员分享,可在线阅读,更多相关《JavaScript常用内置函数.docx(11页珍藏版)》请在三一办公上搜索。

1、JavaScript常用内置函数常用内置函数 1. Math数学对象 调用方式: Math.方法名(参数1,参数2,.); Math.属性; 两个重要的方法:random , round 练习: 产生指定范围的随机数67-99 整数,如m-n,算法为:(公式)Math.random*(n-m+1)+m 四舍五入 -12.4 -12.8 12.4 12.8 代码示例: var m = 67; var n = 99; var result_num = Math.random*(n-m+1)+m; document.write(+Math.floor(result_num)+) document.w

2、rite(+Math.round(-12.8)+) function selectFrom(lower, upper) var sum = upper - lower + 1; /总数-第一个数+1 return Math.floor(Math.random * sum + lower); 2. Date日期对象 var dateTime=new Date; dateTime.方法名 练习: 获取当前系统日期 计算距今天280天后,是XXXX年XX月XX日? 代码示例: var date=new Date; document.write(+date.getFullYear+date.getDa

3、te+日) date.setdate(date.getDate+280); document.write(+date.getFullYear+date.getDate+日) 年+(date.getMonth+1)+月年+(date.getMonth+1)+月3. URI编码函数escape和unescape 作用:当用户从一个页面跳转向另一个页面,同时需要传递信息,而此时的方法使用的是get,传递的信息中存在非文本数字字符,此时需要用户对URL进行解码 练习: var str=“JavaScript字符串”; document.write(“编码前”+str+”); str=escape(st

4、r); document.write(“编码后”+str+”); document.write(“解码后”+unescape(str) +”); 练习:四海兴唐!&JavaWeb.Com 注意:实际开发中,使用的是 encodeURI,decodeURI,encodeURIComponent,decodeURIComponent(处理特殊字符) js中escape,encodeURI,encodeURIComponent三个函数的区别 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,

5、decodeURIComponent 1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。 例如:document.write(a href=http:/ 2、 进行url跳转时可以整体使用encodeURI 例如:Location.href=encodeURI( 3、 js使用数据时可以使用escape Huoho.Com编辑 例如:搜藏中history纪录。 4、 escape对0-255以外的unicode值进行编码时输出%u*格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。 最多使用

6、的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持 escape不编码字符有69个:*,+,-,.,/,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,(,),*,+,,,-,.,/,:,;,=,?,_,0-9,a-z,A-Z encodeURIComponent不编码字符有71个:!, ,(,),*,-,.,_,0-9,a-z,A-Z 提示:因为encodeURIComponent编码比encodeURI编码来

7、的更加彻底,一般来说 encodeURIComponent使用频率要高一些。 var str = 街角的!#:祝福IDO ; var es_str=escape(str); document.write(+str+); document.write(escape(str)编码方式:+es_str+); var de_str=encodeURI(str); document.write(encodeURI(str)编码方式:+de_str+); var en_str=encodeURIComponent(str); document.write(decodeURIComponent(str)编码

8、方式:+en_str+); 4. 动态执行代码eval 作用:是把一个字符串当作JavaScript语句来执行 var str=“window.alert(eval运行)”; document.write(str); eval(str); 演示: eval(var box = 100); /解析了字符串代码 alert(box); eval(alert(100); /同上 eval(function box return 123); /函数也可以 alert(box); 注意: eval方法的功能非常强大,但也非常危险。因此使用的时候必须极为谨慎。特别是在用户输入数据的情况下,非常有可能导致程

9、序的安全性,比如代码注入等等。 说明:除了解析JSON对象外,eval在程序中很少使用,JSON var json=eval(username:zhangsan,userage:16); /json数据格式 document.write(json.username); document.write(); document.write(json.userage); . isNaN函数 格式:isNaN 作用:JavaScript提供了判断表达式是不是数值。上面格式中参数exp是需要判断的表达式,如果表达式的值是NaN,函数返回true;否则返回false 示例: var str1=“www”,s

10、tr2=“555”; val1=parseInt(str1),val2=parseInt(str2); if(isNaN(val1) alert(“str1内容不是数值”); else alert(“str2内容不是数值”) . window对象方法 /var flag = confirm(你是否删除数据?); /返回的是boolean /var mytxt = prompt(我显示的内容,请输入内容);/返回的是输入内容 self对象与window对象完全相同,self通常用于确认就是在当前的窗体内。 window与self对象 window的子对象 JavaScript document

11、对象,下节课介绍 JavaScript frames 对象 JavaScript history 对象 JavaScript location 对象 JavaScript navigator 对象 JavaScript screen 对象 window函数索引 外网地址: 窗体焦点控制函数 JavaScript focus 函数 JavaScript blur 函数 新建窗体函数 JavaScript open 函数 JavaScript close 函数 JavaScript opener 属性 对话框函数 JavaScript alert 函数 JavaScript confirm 函数

12、JavaScript prompt 函数 状态栏属性 JavaScript window.defaultStatus 属性 JavaScript window.status 属性 时间等待与间隔函数 JavaScript setTimeout 函数 JavaScript clearTimeout 函数 JavaScript setInterval 函数 JavaScript clearInterval 函数 由于方法过多,在这里我们只讲几个常用的: l 打开消息框 window.alert(“msg”); l 打开确认对话框var rtnVal=window.confirm(“cfmMsg”)

13、; l 打开输入对话框var str=window.prompt (strShow,strInput); l 打开一个新窗口 var win=window.open (“url”,”sinName”,”param”); l 页面切换,跳转到一个新的页面 window.location=“url”; l 关闭窗口:window.close; 在火狐下window.close方法不好用,实际开发时解决方法: 练习:点击一个按钮,调用一个方法(函数),弹出一个新窗口,在新窗口有一个关闭按钮,点击后可以关闭该窗口 window.open(page.html, newwindow,width=500,h

14、eight=300,left=10,top=20,location=yes,status=yes); function closeWin window.opener=null; window.open(,_self); window.close; 延时程序 l setTimeout方法 功能:可以指定页面中的某段JavaScript代码或者某个函数在指定的时间段后执行 var timeId = setTimeout(code,millisec); 调用javascript脚本的示例 模版 var timeId; function start1 var i = document.getEleme

15、ntById(mytxt).value; document.getElementById(mytxt).value=+i; timeId = window.setTimeout(start1,1000); function stop1 window.clearTimeout(timeId); / window.setTimeout(window.open(01-Math象.html,win1,width=400,height=400),3000); : var i=0; alert(i); setTimeout(i+=1;alert(i),1000); 对var timeId = setTim

16、eout(func,millisec) ; 调用方法的示例: 练习:使用调用方法的形式3秒钟后弹出一个新的窗口 新的窗口5秒后自动关闭. timeId:返回定时器的ID值,用于clearTimeout(timeId)方法停止 定时器 code:在定时时间到时要执行的JavaScript代码串。 指定的func:调用的方法名称 millisec:设定的定时时间,用毫秒数表示。 setInterval方法 作用:按照指定的周期来调用函数或计算表达式。 语法:var timeId=setInterval(code, millisec); var timeId=setInterval(funcname

17、, millisec); timeId:返回定时器的ID值,可用于clearInterval(timeId)方法停止 指定的定时器 code:在定时时间到时要执行的JavaScript代码串。 millisec:设定的定时时间,用毫秒数表示。 示例: setIntervalclearInterval例子 延时程序的终止方法 l setTimeout设定的延时程序终止方法: window. clearTimeout(timeId); l setInterval设定的延时程序终止方法: window.clearInterval(timeId); 总结: setInterval方法与setTimeo

18、ut方法的语法相同,而区别在于,前者会在指定的时间间隔自动重复执行指定的函数或代码段,后者则在指定的时间间隔执行一次指定的函数或代码段 window 常用子对象: history location JavaScript使用手册.chm function on_open var popup_width = 400; var popup_height = 350; var popup_left = (screen.width - popup_width) / 2; var popup_top = (screen.height - popup_height) / 2; var popup_scrol

19、lbars = no; var popup_property = width= + popup_width; var popup_property = popup_property + ,height= + popup_height; var popup_property = popup_property + ,left= + popup_left; var popup_property = popup_property + ,top= + popup_top; var popup_property = popup_property + ,scrollbars= + popup_scrollbars; window.open(url,zipcode,popup_property);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号