JavaScript Tutorial/Window/open
Версия от 18:52, 25 мая 2010; (обсуждение)
Содержание
- 1 Open new window
- 2 Open new window with "_blank" setting
- 3 Open new window with "toolbar=no, status=no, width=200,height=200"
- 4 Open window in different styles
- 5 Popup window with window.open
- 6 This example pops up a new window and then writes a string to it
- 7 Using document.write() on Another Window
- 8 Using document.write() on the Current Window
Open new window
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function new_win()
{
window.open("http://www.wbex.ru","mywin","width=400,height=300,screenX=50,left=50,screenY=50,top=50,status=yes,menubar=yes");
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform">
<INPUT TYPE="button" value="Open New Window" onClick="new_win()">
</FORM>
</BODY>
</HTML>
Open new window with "_blank" setting
<html>
<head>
<script language="JavaScript" type = "text/javascript">
<!--
function GreetingWin()
{
DisplayGreeting = window.open("", "_blank", "toolbar=no, status=no, width=200,height=200");
greeting = "<b>Good Morning!</b>"
DisplayGreeting.document.write(greeting);
}
//-->
</script>
</head>
<body>
<input type="button" value="Click Button" onClick="GreetingWin()">
</body>
</html>
Open new window with "toolbar=no, status=no, width=200,height=200"
<html>
<head>
<script language="JavaScript" type = "text/javascript">
<!--
function GreetingWin()
{
DisplayGreeting = window.open("", "_blank", "toolbar=no, status=no, width=200,height=200");
greeting = "<b>Good Morning!</b>"
DisplayGreeting.document.write(greeting);
}
//-->
</script>
</head>
<body>
<input type="button" value="Click Button" onClick="GreetingWin()">
</body>
</html>
Open window in different styles
<html>
<head>
<title>Open Window</title>
<script language="JavaScript" type =" text/javascript">
<!--
function ChangeLink(currentform)
{
var window_feature = "";
alert(window_feature);
for (var cb=0; cb<currentform.length - 1; cb++) {
if (currentform[cb].checked)
{
currentform[cb].value = 1;
} else {
currentform[cb].value = 0;
}
window_feature = (window_feature + "," + currentform[cb].name + "= [ic:ccc]"+ currentform[cb].value);
}
window.open("http://www.wbex.ru", "_blank", window_feature);
}
//-->
</script>
</head>
<body>
<form name="form1">
<input type="checkbox" name="directories" value="0">Directories Option</p>
<P><input type="checkbox" name="location" value="0">Location Option</p>
<P><input type="checkbox" name="menubar" value="0">Menu Bar</p>
<P><input type="checkbox" name="resized" value="0">Allow Window to be Resized</p>
<P><input type="checkbox" name="scrollbars" value="0">Scrollbars</p>
<P><input type="checkbox" name="status" value="0">Status Bar</p>
<P><input type="checkbox" name="toolbar" value="0">Toolbar</p>
<P><input type="button" value="Create Window" name="CreateWin" onClick="ChangeLink(this.form)"></p>
</form>
</body>
</html>
Popup window with window.open
<html>
<head>
<script language="JavaScript" type = "text/javascript">
<!--
function GreetingWin()
{
DisplayGreeting = window.open("", "_blank", "toolbar=no, status=no, width=200,height=200");
greeting = "<b>Good Morning!</b>"
DisplayGreeting.document.write(greeting);
}
//-->
</script>
</head>
<body>
<input type="button" value="Click Button" onClick="GreetingWin()">
</body>
</html>
This example pops up a new window and then writes a string to it
<html>
<head>
<title>Document Open/Close Example</title>
</head>
<body>
<P>Due to new popup blockers, this code can only run when you click the following button:</p>
<P><input type="button" value="Click Me" onclick="showPopup()" />
<script type="text/javascript">
function showPopup() {
var oNewWin = window.open("about:blank", "newwindow", "height=150,width=300,top=10,left=10,resizable=yes");
oNewWin.document.open();
oNewWin.document.write("<html><head><title>New Window</title></head>");
oNewWin.document.write("<body>This is a new window!</body></html>");
oNewWin.document.close();
}
</script>
</body>
</html>
Using document.write() on Another Window
<html>
<head>
<title>Writing to Subwindow</title>
<script type="text/javascript">
var newWindow;
function makeNewWindow() {
newWindow = window.open("","","status,height=200,width=300");
}
function subWrite() {
if (newWindow.closed) {
makeNewWindow();
}
newWindow.focus();
var newContent = "<html><head><title>title</title></head>";
newContent += "<body bgcolor="coral"><h1>This document is brand new.</h1>";
newContent += "</body></html>";
newWindow.document.write(newContent);
newWindow.document.close();
}
</script>
</head>
<body onload="makeNewWindow()">
<form>
<input type="button" value="Write to Subwindow" onclick="subWrite()">
</form>
</body>
</html>
Using document.write() on the Current Window
<html>
<head>
<title>Writing to Same Doc</title>
<script type="text/javascript">
function reWrite() {
var newContent = "<html><head><title>title</title></head>";
newContent += "<body bgcolor="aqua"><h1>This document is brand new.</h1>";
newContent += "Click the Back button to see original document.";
newContent += "</body></html>";
document.write(newContent);
document.close();
}
</script>
</head>
<body>
<form>
<input type="button" value="Replace Content" onclick="reWrite()">
</form>
</body>
</html>