基础实例write()document.write(“this is a string”);//生成普通文本document.write("<br>"+Date());//html+函数document.write("<input type=‘text’>");//html,生成标签JS代码块 JS中没有块作用域,只有函数作用域。JS代码块只是把需要一起执行的语句进行分组。prompt()var name = prompt(“please input your name:”,“Anne”)//后面的参数为输入框的默认值confirm()var yourChoice = confirm(“please confirm your choice!”)//yourChoice == true,选择了“确认”//yourChoice == false,选择了“取消”break 中断,switch,if等语句continue 跳出本次循环,继续下次循环for in 遍历数组内元素for(index in Array)//index为数组内元素索引,从0开始onerror事件 捕获网页中的错误。(chrome、opera、safari 浏览器不支持) 只要页面中出现脚本错误,就会产生 onerror 事件。如果需要利用 onerror 事件,就必须创建一个处理错误的函数。你可以把这个函数叫作 onerror 事件处理器 (onerror event handler)。这个事件处理器使用三个参数来调用:msg(错误消息)、url(发生错误的页面的 url)、line(发生错误的代码行)。<html><head><script type=“text/javascript”>onerror=handleErrvar txt=““function handleErr(msg,url,l){txt=“There was an error on this page.\n\n"txt+=“Error: " + msg + “\n"txt+=“URL: " + url + “\n"txt+=“Line: " + l + “\n\n"txt+=“Click OK to continue.\n\n"alert(txt)return true}function message(){adddlert(“Welcome guest!”)}</script></head><body><input type=“button” value=“View message” onclick=“message()” /></body></html>高级实例