JS中常用window屬性和方法



對象在編寫時可不使用window這個前綴

setInterval() – 間隔指定的毫秒數不停地執行指定的代碼。

clearInterval() – 用於停止 setInterval() 方法執行的函數代碼。

setTimeout() - 暫停指定的毫秒數后執行指定的代碼

clearTimeout() - 用於停止執行setTimeout()方法的函數代碼

例:

使用計時器在頁面顯示時鍾
•時間格式為:xxxx年xx月xx日 xx:xx:xx 到秒
•每秒刷新一次

1 <body>
2     <button onclick="open1()">打開新頁面</button>
3     <button onclick="start1()">開始顯示時間</button>
4     <button onclick="stop1()">停止時間</button>
5 </body>

 

<script type="text/javascript">
            function open1(){
                window.open("new_file.html","newFile","menubar=no,location=no,toolbar=no,resize=no,width=500,height=500,top=200,left=400")
            }
            
            function time1(){
            var date = new Date();
            var y = date.getFullYear();
            var mo = date.getMonth();
            var d = date.getDate();
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();
            console.log("%d年%d月%d日 %d:%d:%d" ,y , mo , d , h , m , s );                
            }
            var inter = null ;
            
            function start1(){
                if(inter != null){
                    stop1();
                }
                inter = setInterval(time1,1000);
            }
            
            function stop1(){
                clearInterval(inter);
                inter = null;
            }
            
            
        </script>

close() - 關閉當前窗口

open() - 打開新窗口,並返回新窗口的對象

語法  window.open(URL,name,features,replace);

URL:可選字符串,聲明了新窗口的URL。如果省略這個參數或者值為空字符串,則新窗口不顯示任何文檔

name:可選字符串,是一個由逗號分割的特征列表,它聲明了新窗口名稱。如果此參數指定已存在窗口,則open方法返回對指定窗口的     引用(不再創建新窗口)。這時,features將被忽略。

features:可選字符串,聲明了新窗口顯示的標准瀏覽器特征,如果省略,則新窗口具有所有標准特征。

replace:一個可選的布爾值。規定了裝載到窗口的 URL 是在窗口的瀏覽歷史中創建一個新條目,還是替換瀏覽歷史中的當前條目。支持下面的值:•true - URL 替換瀏覽歷史中的當前條目。•false - URL 在瀏覽歷史中創建新的條目。

其中open() 方法的第三個參數如下表

channelmode=yes|no|1|0

是否使用劇院模式顯示窗口。默認為 no。

resizable=yes|no|1|0

窗口是否可調節尺寸。默認是 yes。

directories=yes|no|1|0

是否添加目錄按鈕。默認為 yes。

scrollbars=yes|no|1|0

是否顯示滾動條。默認是 yes。

fullscreen=yes|no|1|0

是否使用全屏模式顯示瀏覽器。默認是 no。

處於全屏模式的窗口必須同時處於劇院模式。

status=yes|no|1|0

是否添加狀態欄。默認是 yes。

height=pixels

窗口文檔顯示區的高度。以像素計。

titlebar=yes|no|1|0

是否顯示標題欄。默認是 yes。

left=pixels

窗口的 x 坐標。以像素計。

toolbar=yes|no|1|0

是否顯示瀏覽器的工具欄。默認是 yes。

location=yes|no|1|0

是否顯示地址字段。默認是 yes。

top=pixels

窗口的 y 坐標。

menubar=yes|no|1|0

是否顯示菜單欄。默認是 yes。

width=pixels

窗口的文檔顯示區的寬度。以像素計。

Location對象屬性

hash

設置或返回從井號 (#) 開始的 URL(錨)。

host

設置或返回主機名和當前 URL 的端口號。

hostname

設置或返回當前 URL 的主機名。

href

設置或返回完整的 URL。

pathname

設置或返回當前 URL 的路徑部分。

port

設置或返回當前 URL 的端口號。

protocol

設置或返回當前 URL 的協議。

search

設置或返回從問號 (?) 開始的 URL(查詢部分)。

以下為示例
 1 <script type="text/javascript">
 2 var hash = location.hash;// top
 3 var host = location.host;// www.baidu.com:8020
 4 var hostname = location.hostname;// www.baidu.com
 5 var port = location.port;// 8020;
 6 var pathname = location.pathname;// index.html
 7 var protocol = location.protocol; // http
 8 console.log(location);
 9 console.log(hash);
10 console.log(host);
11 console.log(hostname);
12 console.log(port);
13 console.log(pathname);
14 console.log(protocol);
15 </script>

Location 對象方法

assign()  加載新的文檔。

reload()  重新加載當前文檔,相當於刷新頁面。

replace()  用新的文檔替換當前文檔。

(location.replace("location.html#top?a=10&b=20");相當於location.href = "location.html#top?a=10&b=20";)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM