void运算符
<a href="javascript:void(0)" onclick="window.alert('test code')" /> //既保存超链接样式,点击之后执行一段js代码,同时又不能完成页面跳转 //void(表达式)执行表达式,但是不返回任何结果
常见控制语句
js特殊的控制语句
for...in...
with语句
<script type="text/javascript"> //js for..in..的两个使用场景 //场景一:对象 alert("场景1:对象"); function User(username, password) { this.username = username; this.password = password; } var user = new User("xun", "just a test"); for(var value in user){ alert(typeof value); alert(value); alert(user[value]); } //场景二:数组 alert("场景2:数组"); var arry = [false, true, 3.14, "test"]; for(var index in arry){ alert(typeof index); alert(index); alert(arry[index]); } //js with(带上)语句 with(user){ alert(username + "," + password); //相当于:alert(user.username + "," + user.password) } </script>
<!DOCTYPE html> <html lang="en"> <head> <title>js内置数组对象</title> <meta charset="UTF-8"> </head> <body> <script> //js内置数组对象Array var array1 = new Array(); alert(array1.length); var array2 = new Array(3); alert(array2.length); var array3 = new Array(2, 3); alert(array3.length); //常用:var array = []; //特点:自动扩容 //内置数组对象常用方法 var array4 = array3.reverse(); //数组3自身元素也发生了反转 alert("array3: " + array3.join("-")); alert("array4: " + array4.join("-")); //js内置数组对象可以模拟数据栈 array3.push(1); alert("array3: " + array3.join("-")); var lastElt = array3.pop(); alert("last element: " + lastElt); </script> </body> </html>
window.open 与window.close
window.confirm()确认框
window.history.
window.location.href 或者 document.location.href (href可以去掉)
设置当前窗口为顶级窗口
if(window.top != window.self){ window.top.location = window.self.location }
<!DOCTYPE html> <html lang="en"> <head> <title>eval函数</title> <meta charset="UTF-8"> </head> <body> <script> var jsonData = "{\"username\":\"zhangsan\", \"password\":\"123\"}"; window.eval("var jsonObj = " + jsonData); alert(jsonObj.username + "," + jsonObj.password); </script> </body> </html>