JavaScript 瀏覽器對象

window對象是BOM的核心,window對象指當前的瀏覽器窗口。

1.打開窗口

window.open(('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');//打開新窗口

window.open 彈出新窗口的命令;
page.html 彈出窗口的文件名;
newwindow 彈出窗口的名字(不是文件名),非必須,可用空”代替;
height=100 窗口高度;
width=400 窗口寬度;
top=0 窗口距離屏幕上方的象素值;
left=0 窗口距離屏幕左側的象素值;
toolbar=no 是否顯示工具欄,yes爲顯示;
menubar,scrollbars 表示菜單欄和滾動欄。
resizable=no 是否允許改變窗口大小,yes爲允許;
location=no 是否顯示地址欄,yes爲允許;
status=no 是否顯示狀態欄內的信息(通常是文件已經打開),yes爲允許;

2.計時器

setInterval(代碼,交互時間);

<script type="text/javascript">
        var int = setInterval(clock, 2000)
    function clock() {
        var time = new Date();
        document.getElementById("clock").value = time;
    }
</script>
<input type="text" id="clock" size="50" />

取消計時器clearInterval()

<script type="text/javascript">
    function clock() {
            var time=new Date();                     
                document.getElementById("clock").value = time;
    }
        var i=setInterval("clock()",1000);
</script>
<input type="text" id="clock" size="50" />
<input type="button" value="Stop" onclick="clearInterval(i)"  />

setTimeout(代碼,延遲時間);

<script type="text/javascript">
    setTimeout("alert('Hello!')", 3000 );
</script>

setTimeout()和clearTimeout()一起使用,停止計時器。

<script type="text/javascript">
    var num = 0;
    var i;
    function startCount() {
        document.getElementById('count').value = num;
        num = num + 1;
        i = setTimeout("startCount()", 1000);
    }
    function stopCount() {
        clearTimeout(i);
    }
</script>
<input type="button" value="Start" id="count" onClick="startCount()"/>
<input type="button" value="Stop" onClick="stopCount()"/>

3.history對象

history對象記錄了用戶曾經瀏覽過的頁面(URL),並可以實現瀏覽器前進與後退相似導航的功能。

注意:從窗口被打開的那一刻開始記錄,每個瀏覽器窗口、每個標籤頁乃至每個框架,都有自己的history對象與特定的window對象關聯。

<script type="text/javascript">
      var HL = window.history.length   ;
      document.write(HL);
      function last(){
        var last = window.history.back();
      }
      function next(){
        var next = window.history.forward();
      }
      function go(){
        var go = window.history.go("number");//number爲瀏覽器前歷史記錄數值如返回上一頁則爲go(1);
      }
</script>

4.Location對象

location用於獲取或設置窗體的URL,並且可以用於解析URL。

alert(window.location.href)

5.Navigator對象

Navigator 對象包含有關瀏覽器的信息,通常用於檢測瀏覽器與操作系統的版本。

var browser=navigator.appName;
var b_version=navigator.appVersion;
document.write(browser);
document.write("<br>");
document.write(b_version);

6.userAgent

返回用戶代理頭的字符串表示(就是包括瀏覽器版本信息等的字符串)

var u_agent = navigator.userAgent; 
var B_name="Failed to identify the browser"; 
if(u_agent.indexOf("Firefox")>-1){ 
    B_name="Firefox"; 
}else if(u_agent.indexOf("Chrome")>-1){ 
    B_name="Chrome"; 
}else if(u_agent.indexOf("MSIE")>-1&&u_agent.indexOf("Trident")>-1){ 
    B_name="IE(8-10)";  
}
  document.write("B_name:"+B_name+"<br>");
  document.write("u_agent:"+u_agent+"<br>");

7.screen對象

screen對象用於獲取用戶的屏幕信息。

alert(  window.screen.屬性)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章