當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
JS 打开新窗口
                            
                            
                            使用JavaScript window對象(屬于BOM)打開新窗口:
語法:
window.open(URL,name,features);
該方法用于打開一個新的瀏覽器窗口或查找一個已命名的窗口。
 
參數說明:
  
 
 
  
 
1、打開新的窗口,窗口特性使用默認值
<body> <script type="text/javascript">function openAnother() {window.open("http://www.baidu.com"); //打開新窗口,窗口特性全部使用默認值。} </script> <button οnclick="openAnother()">打開新窗口</button> </body>
2、打開新的窗口,設置窗口特性
<body><button οnclick="openAnother();">打開新窗口</button><script type="text/javascript">var win;function openAnother () {win = window.open("http://www.baidu.com", "", "width=400, height=400,left = 200, right = 200, toolbar = no, menubar = no,location=no,resizable=no");//有些特性在個別瀏覽器上已被進制修改。}</script> </body>
3、關閉新打開的窗口
 
 
                            
                        
                        
                        語法:
window.open(URL,name,features);
該方法用于打開一個新的瀏覽器窗口或查找一個已命名的窗口。
參數說明:
URL :一個可選的字符串,聲明了要在新窗口中顯示的文檔的 URL。如果省略了這個參數,或者它的值是空字符串,那么新窗口就不會顯示任何文檔。
 
name :一個可選的字符串,該字符串是一個由逗號分隔的特征列表,其中包括數字、字母和下劃線,該字符聲明了新窗口的名稱。這個名稱可以用作標記<a> 和 <form> 的屬性 target 的值。如果該參數指定了一個已經存在的窗口,那么open()方法就不再創建一個新窗口,而只是返回對指定窗口的引用。在這種情況下,features 將被忽略。
 
窗口特征(常用):
| height、width | 窗口文檔顯示區的高度、寬度。以像素計。 | 
| left、top | 窗口的x坐標、y坐標。以像素計 | 
| toolbar=yes | no | 1 | 0 | 是否顯示瀏覽器的工具欄。黙認是yes。 | 
| scrollbars=yes | no | 1| 0 | 是否顯示滾動條。黙認是yes。 | 
| location=yes | no | 1 | 0 | 是否顯示地址地段。黙認是yes。 | 
| status=yes | no | 1 | 0 | 是否添加狀態欄。黙認是yes。 | 
| menubar=yes | no | 1 | 0 | 是否顯示菜單欄。黙認是yes。 | 
| resizable=yes | no | 1 | 0 | 窗口是否可調節尺寸。黙認是yes。 | 
| titlebar=yes | no | 1 | 0 | 是否顯示標題欄。黙認是yes。 | 
| fullscreen=yes | no | 1 | 0 | 是否使用全屏模式顯示瀏覽器。黙認是no。 | 
注:不同的游覽器對窗口特征屬性支持也不相同
 
1、打開新的窗口,窗口特性使用默認值
<body> <script type="text/javascript">function openAnother() {window.open("http://www.baidu.com"); //打開新窗口,窗口特性全部使用默認值。} </script> <button οnclick="openAnother()">打開新窗口</button> </body>
2、打開新的窗口,設置窗口特性
<body><button οnclick="openAnother();">打開新窗口</button><script type="text/javascript">var win;function openAnother () {win = window.open("http://www.baidu.com", "", "width=400, height=400,left = 200, right = 200, toolbar = no, menubar = no,location=no,resizable=no");//有些特性在個別瀏覽器上已被進制修改。}</script> </body>
3、關閉新打開的窗口
open方法返回打開的那個窗口的window對象,可以調用close方法關閉新打開的窗口。
<body> <button οnclick="openAnother();">打開新窗口</button> <button οnclick="closeAnother();">關閉新打開的窗口</button> <script type="text/javascript">var win;function openAnother () {win = window.open("http://www.baidu.com", "", "width=400, height=400,left = 200, right = 200, toolbar = no, menubar = no,location=no,resizable=no");}function closeAnother() {win.close();//關閉指定窗口} </script> </body>
總結
                            
                        - 上一篇: Axure RP8导入文件报错
 - 下一篇: 2018 Multi-Universit