1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript-打開新窗口(window.open)</title> 6 <script type="text/javascript"> 7 window.open('https://www.cnblogs.com/dhnblog/','_blank','width=300,height=300,menubar=no,toobar=no,status=no,scrollbars=yes'); 8 // window.open('https://www.cnblogs.com/dhnblog/','_self','width=300,height=100,menubar=no,toobar=no,status=no,scrollbars=yes'); 9 //window.open('https://www.cnblogs.com/dhnblog/','_top','width=300,height=300,menubar=no,toobar=no,status=no,scrollbars=yes'); 10 </script> 11 </head> 12 <body> 13 </body> 14 </html>
代碼含義:打開https://www.cnblogs.com/dhnblog/博客,大小為300px*200px,無菜單,無工具欄,無狀態欄,有滾動條窗口;其中open() 方法可以查找一個已經存在或者新建的瀏覽器窗口。
語法結構:
window.open([URL], [窗口名稱], [參數字符串])
參數說明:
URL:可選參數,在窗口中要顯示網頁的網址或路徑。如果省略這個參數,或者它的值是空字符串,那么窗口就不顯示任何文檔。
窗口名稱:可選參數,被打開窗口的名稱。
- 該名稱由字母、數字和下划線字符組成。
- "_top"、"_blank"、"_self"具有特殊意義的名稱;A._blank:在新窗口顯示目標網頁B._self:在當前窗口顯示目標網頁C._top:框架網頁中在上部窗口中顯示目標網頁
- 相同 name 的窗口只能創建一個,要想創建多個窗口則 name 不能相同。
- name 不能包含有空格。
- 參數字符串:可選參數,設置窗口參數,各參數用逗號隔開,引號包裹*運行結果考慮瀏覽器兼容問題。
e.g:打開https://www.cnblogs.com/dhnblog/網頁,將在新窗體中打開,寬為600,高為400,距屏頂100像素,屏左0像素;當點擊按鈕時,打開新窗口。
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>window.open</title> 6 <script type="text/javascript"> 7 function Wopen(){ 8 window.open('https://www.cnblogs.com/dhnblog/','dhn','width=600,height=400,top=100px,left=0,menubar=no,toobar=no,status=no,scrollbars=yes'); 9 } 10 </script> 11 </head> 12 <body> 13 <input name="button" type="button" onClick="Wopen()" value="點擊我,打開新窗口!" / > 14 </body> 15 </html>
總結:注意window.open('https://www.cnblogs.com/dhnblog/','dhn','width=600,height=400,top=100px,left=0,menubar=no,toobar=no,status=no,scrollbars=yes');里面的這個number值是要用等號的,如果你用的是這個分號,像這樣的話window.open('https://www.cnblogs.com/dhnblog/','dhn','width:600,height:400,top:100px,left:0,menubar=no,toobar=no,status=no,scrollbars=yes');不管你怎么修改number,你發現這個在新窗口顯示目標網頁和這個目標網頁真沒啥區別,這時你心里是不是有些懷疑人生了...