1、函數聲明
function add(a, b) {
return a + b;
}
var sum = add(1 , 3);
alert(sum);
function add(){
return arguments[0]+arguments[1];
}
alert(add(2,3));
// 局部 內部定義的,外部能夠訪問。外部定義的,內部能夠訪問。
function add(){
var a=0;
function test(){
alert(a);
}
test();
}
add();
//預解析 做用域 讀var a和function
var a=1;
function add(){
alert(a);//不加var a=2;結果爲1(做用鏈往上找),不然爲undefined
// var a=2;
alert (a);//加var a=2;結果爲2
}
add();
// 回調函數 fun是引用
function math(a,b,fun){
return fun(a,b);
}
alert(math(2,3,add));
function add(a,b){
return a+b;
}
function jianfa(a,b){
return a-b;
}
//匿名函數
alert((function(a,b){ return a+b; })(2,3));//結果爲5
var a=function(){
alert(0);
}
var b=a;
b();
//遞歸 5的階乘
function test(sum){
if(sum==1){
return sum;
}
else{
return sum*test(--sum);
}
}
alert (test(5));
2、BOM
1.操做窗口屬性
IE CHORM:
window.screenleft瀏覽器距屏幕左側距離
window.screentop瀏覽器距屏幕頂部距離
IE FOX:
window.screenX瀏覽器距屏幕左側距離
window.screenY瀏覽器距屏幕頂部距離
瀏覽器的左側和頂部的邊距
alert((window.screenLeft||window.screenX) + " " + (window.screenTop||window.screenY));
2.瀏覽器尺寸
IE CHORM(不許):
window.innerWidth :獲取瀏覽器的寬度,不計算標題欄
window.innerHeight:獲取瀏覽器的高度,不計算標題欄
IE :
document.documentElement.clientWidth
document.documentElement.clientHeight
//瀏覽器的尺寸
//FF,CHROM
// alert(window.innerWidth + " " + window.innerHeight);
//IE
// alert(document.documentElement.clientWidth + " " + do cument.documentElement.clientHeight);
3.窗口移動和改變尺寸的方法(IE有效)
window.moveBy(x,y)相對於當前位置
window.moveTo(x,y)相對於屏幕
window.resizeBy(x, y)相對於自身水平垂直展開
window.resizeTo(x, y);固定尺寸
4.滾動條控制
scrollBy(x,y)相對於自己的位置,x 水平方向, y 垂直方向
scrollTo(x,y)相對於原點進行偏移
5.打開新窗口
window.open(URL,name,features,replace)
features:
height窗口文檔顯示區的高度。以像素計。,
left窗口的 x 座標。以像素計。
width窗口的文檔顯示區的寬度.
top 窗口的 y 座標。
scrollbars=yes|no|1|0 是否顯示滾動條。默認是 yes。
titlebar=yes|no|1|0 是否顯示標題欄。默認是 yes。
toolbar=yes|no|1|0 是否顯示瀏覽器的工具欄。默認是 yes。
location=yes|no|1|0 是否顯示地址字段。默認是 yes。
function openwin()
{
OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no ,scrollbars="+scroll+",menubar=no");
6.setInterval(function(){
code要調用的函數或要執行的代碼串
}, times);
times:週期性執行或調用 code 之間的時間間隔,以毫秒計。
返回值
一個能夠傳遞給 Window.clearInterval() 從而取消對 code 的週期性執行的值。
時間倒數例子:
<script>
window.onload=function(){
var txt=document.getElementById("txt");
var i=5;
var times=setInterval(function(){
i--;
txt.innerHTML=i;
},1000);
setTimeout(function(){
window.clearInterval(times);
window.open("./open.html", "_blank", "", "width=300px,height=300px,left=500px,top=300px")
},6000);
}
</script>
<body>
<span>倒計時</span> <span id="txt"></span>
</body>html
3、 history 訪問歷史頁面 length/go()/forward()/back()
屬性:
length 表已防問的頁面個數
示例:
alert(window.history.length);
方法:
go()方法 注:當值爲正數,向前進相應的數,當爲0,即刷新
forward()方法 表返回
back()方法 表返回
示例:
window.onload=function(){
var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");
var button3 = document.getElementById("button3");
button1.onclick=function(){
//訪問歷史頁面 history 的go()方法 注:當值爲正數,向前進相應的數,當爲0,即刷新
window.history.go(1);
};
button2.onclick=function(){
//訪問歷史頁面 history 的forward()方法 表返回
window.history.forward();
};
button3.onclick=function(){
//訪問歷史頁面 history 的back()方法 表返回
window.history.back();
};
4、 location href/search/assign()/replace()/reload()
屬性:
href屬性 (設置/返回一個完整的URL)
示例:
//location 的href屬性(返回一個完整的URL)
alert(window.location.href);
alert(1);
//location 的href屬性(設置一個完整的URL)
window.location.href="dom2.html";
search屬性 (獲取URL後面的查詢數據)
示例:
//location 的search屬性(獲取URL後面的查詢數據)
alert(window.location.search);
方法: assign方法:頁面跳轉
location 的assign()方法:頁面跳轉
window.location.assign("dom2.html");
location 的replace()方法:頁面跳轉(無歷史記錄)
location 的reload()方法:頁面刷新
示例:
var button = document.getElementById("button");
button.onclick=function(){
window.location.reload();
}
5、screen:客戶端顯示屏幕信息 availHeight/availWidth/height/windth
screen 的 availHeight屬性:返回顯示屏幕的高度(除任務欄)
alert(window.screen.availHeight);
screen 的 availWidth屬性:返回顯示屏幕的寬度
alert(window.screen.availWidth);
screen 的 Height屬性:包含任務欄的高度
alert(window.screen.availHeight);
screen 的 Width屬性:包含任務欄的寬度
alert(window.screen.availWidth);瀏覽器