Window.Open details

1. window.open() support environment:
JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+

2. Basic syntax:
window.open(pageURL,name,parameters) 
Among them:
pageURL is the child window path 
name is child Window handle 
parameters are window parameters (each parameter is separated by a comma) 

3. Example:

<SCRIPT> 
<!-- 
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') 
//写成一行 
--> 
</SCRIPT>


  After the script runs, page.html will be opened in a new window newwindow, with a width of 100, a height of 400, 0 pixels from the top of the screen, 0 pixels from the left of the screen, no toolbar, no menu bar, no scroll bar, no Resize, no address bar, no status bar. Please check.
  The above example involves several commonly used parameters, in addition to many other parameters, please see four.

4. Various parameters
  Among them, yes/no can also use 1/0; pixel value is a specific value, the unit is pixel.

Parameter | Value range | Description 

alwaysLowered | yes/no | Specifies that the window is hidden behind all windows 
alwaysRaised | yes/no | Specifies that the window is suspended above all windows 
depended | yes/no | Whether to close the parent window at the same time 
directories | yes/ no | Whether the directory bar of Nav2 and 3 is visible 
height | pixel value | Window height 
hotkeys | yes/no | Set safe exit hotkey in windows without menu bar 
innerHeight | pixel value | The pixel height of the document in the window 
innerWidth | pixel value | The pixel width of the document in the window 
location | yes/no | whether the location bar is visible 
menubar | yes/no | whether the menu bar is visible 
outerHeight | pixel value | set the pixel height of the window (including decorative borders) 
outerWidth | pixel value | set The width in pixels of the window (including decorative borders) 
resizable | yes/no | whether the window can 
be resized screenX | pixel value | the pixel length of the window from the left border of the screen 
screenY | pixel value | the pixel length of the window from the upper border of the screen 
scrollbars | yes/no | whether the window can have a scroll bar 
titlebar | yes/no | whether the window title bar is visible 
toolbar | yes/no | whether the window toolbar is visible 
Width | pixel value | the pixel width of the window 
z-look | yes/no | whether the window floats on top of other windows when activated

= ===================================================== ==

[1, the most basic pop-up window code] 
  In fact, the code is very simple: 

<SCRIPT LANGUAGE="javascript"> 
<!-- 
window.open ('page.html') 
--> 
</SCRIPT>


   Since these are javascripts, they should be placed between the <SCRIPT LANGUAGE="javascript"> tags and </script>. <!-- and --> work on some older browsers, where the code in the tag will not be displayed as text. To develop this good habit ah. 
  Window.open ('page.html') is used to control the pop-up new window page.html. If page.html is not in the same path as the main window, the path, absolute path (http://) and relative path should be written in front. Paths (../) are fine. Both single and double quotes are fine, just don't mix them. 
  This piece of code can be added anywhere in HTML, between <head> and </head>, or between <body> and </body>. The earlier the code is, the earlier it is executed, especially if the page code is long, and you want the page to pop up earlier Just move forward as much as possible. 

【2. Pop-up window after setting】  Let's talk about the setting of pop-up window. Just add a little more to the code above.   Let's customize the appearance, size, and position of the pop-up window to suit the specific situation of the page.  


<SCRIPT LANGUAGE="javascript"> 
<!-- 
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') 
//写成一行 
--> 
</SCRIPT>


Parameter explanation: 
<SCRIPT LANGUAGE="javascript"> js script starts; 
window.open is the command to pop up a new window; 
'page.html' is the file name of the pop-up window; 
'newwindow' is the name of the pop-up window (not the file name), not required , can be replaced by empty ''; 
height=100 window height; 
width=400 window width; 
top=0 the pixel value of the window from the top of the screen; 
left=0 the pixel value of the window from the left side of the screen; 
toolbar=no whether to display tools bar, yes for display; 
menubar, scrollbars for menu bar and scroll bar. 
Resizable=no Whether it is allowed to change the size of the window, yes is allowed; 
location=no is whether to display the address bar, yes is allowed; 
status=no Whether the information in the status bar is displayed (usually the file has been opened), yes is allowed; 
</SCRIPT > The end of the js script 

[3. Use the function to control the pop-up window]   The following is a complete code. 


<html> 
<head> 
<script LANGUAGE="JavaScript"> 
<!-- 
function openwin() { window.open ("page.html", "newwindow", "height=100, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") 
//写成一行 

//--> 
</script> 
</head> 
<body onload="openwin()"> 
…任意的页面内容… 
</body> 
</html>


这里定义了一个函数openwin(),函数内容就是打开一个窗口。在调用它之前没有任何用途。 
怎么调用呢? 
方法一:<body onload="openwin()"> 浏览器读页面时弹出窗口; 
方法二:<body onunload="openwin()"> 浏览器离开页面时弹出窗口; 
方法三:用一个连接调用: 
<a href="#" onclick="openwin()">打开一个窗口</a> 
注意:使用的"#"是虚连接。 
方法四:用一个按钮调用: 
<input type="button" onclick="openwin()" value="打开窗口"> 

【4、同时弹出2个窗口】 

  对源代码稍微改动一下:


<script LANGUAGE="JavaScript"> 
<!-- 
function openwin() 
{ window.open ("page.html", "newwindow", "height=100, width=100, top=0,left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") 
//写成一行 
window.open ("page2.html", "newwindow2", "height=100, width=100, top=100, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") 
//写成一行 

//--> 
</script>


  为避免弹出的2个窗口覆盖,用top和left控制一下弹出的位置不要相互覆盖即可。最后用上面说过的四种方法调用即可。 
注意:2个窗口的name(newwindows和newwindow2)不要相同,或者干脆全部为空。OK? 

【5、主窗口打开文件1.htm,同时弹出小窗口page.html】 

  如下代码加入主窗口<head>区:


<script language="javascript"> 
<!-- 
function openwin() 
{window.open("page.html","","width=200,height=200") 

//--> 
</script>


  加入<body>区: 
<a href="1.htm" onclick="openwin()">open</a>即可。 

【6、弹出的窗口之定时关闭控制】 

  下面我们再对弹出的窗口进行一些控制,效果就更好了。如果我们再将一小段代码加入弹出的页面(注意是加入到page.html的HTML中,可不是主页面中,否则…),让它10秒后自动关闭是不是更酷了? 
  首先,将如下代码加入page.html文件的<head>区:


<script language="JavaScript"> 
function closeit() 
{setTimeout("self.close()",10000) //毫秒} 
</script>


  然后,再用<body onload="closeit()"> 这一句话代替page.html中原有的<BODY>这一句就可以了。(这一句话千万不要忘记写啊!这一句的作用是调用关闭窗口的代码,10秒钟后就自行关闭该窗口。) 

【7、在弹出窗口中加上一个关闭按钮】 

<FORM> 
<INPUT TYPE='BUTTON' VALUE='关闭' onClick='window.close()'> 
</FORM>


  呵呵,现在更加完美了! 

【8、内包含的弹出窗口-一个页面两个窗口】 

  上面的例子都包含两个窗口,一个是主窗口,另一个是弹出的小窗口。 
  通过下面的例子,你可以在一个页面内完成上面的效果。 

<html> 
<head> 
<SCRIPT LANGUAGE="JavaScript"> 
function openwin() 
{OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no"); 
//写成一行 
OpenWindow.document.write("<TITLE>例子</TITLE>") 
OpenWindow.document.write("<BODY BGCOLOR=#ffffff>") 
OpenWindow.document.write("<h1>Hello!</h1>") 
OpenWindow.document.write("New window opened!") 
OpenWindow.document.write("</BODY>") 
OpenWindow.document.write("</HTML>") 
OpenWindow.document.close()} 
</SCRIPT> 
</head> 
<body> 
<a href="#" onclick="openwin()">打开一个窗口</a> 
<input type="button" onclick="openwin()" value="打开窗口"> 
</body> 
</html>


  看看 OpenWindow.document.write()里面的代码不就是标准的HTML吗?只要按照格式写更多的行即可。千万注意多一个标签或少一个标签就会出现错误。记得用OpenWindow.document.close()结束啊。 

【9、终极应用--弹出的窗口之Cookie控制】 

   回想一下,上面的弹出窗口虽然酷,但是有一点小毛病(沉浸在喜悦之中,一定没有发现吧?)比如你将上面的脚本放在一个需要频繁经过的页面里(例如首 页),那么每次刷新这个页面,窗口都会弹出一次,是不是非常烦人?:-(有解决的办法吗?Yes! ;-) Follow me. 
  我们使用cookie来控制一下就可以了。
  首先,将如下代码加入主页面HTML的<HEAD>区: 

<script> 
function openwin() 
{window.open("page.html","","width=200,height=200")} 
function get_cookie(Name) 
{var search = Name + "=" 
var returnvalue = ""; 
if (document.cookie.length > 0) { 
offset = document.cookie.indexOf(search) 
if (offset != -1) { 
offset += search.length 
end = document.cookie.indexOf(";", offset); 
if (end == -1) 
end = document.cookie.length; 
returnvalue=unescape(document.cookie.substring(offset,end)) 


return returnvalue; 

function loadpopup(){ 
if (get_cookie('popped')==''){ 
openwin() 
document.cookie="popped=yes" 


</script>


   然后,用<body onload="loadpopup()">(注意不是openwin而是loadpop啊!)替换主页面中原有的<BODY>这一句 即可。你可以试着刷新一下这个页面或重新进入该页面,窗口再也不会弹出了。真正的Pop-Only-Once! 
  写到这里弹出窗口的制作和应用技巧基本上算是完成了,俺也累坏了,一口气说了这么多,希望对正在制作网页的朋友有所帮助俺就非常欣慰了。
  需要注意的是,JS脚本中的的大小写最好前后保持一致。

 

转自:https://blog.csdn.net/saiyuki/article/details/319658

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326073502&siteId=291194637
Recommended