window 对象的 open() 方法打开一个新的窗口,
该方法有3 个参数:
第一个参数是要加载的URL;
第二个是为新窗口分配的名字,是用于HTML标记中作为target属性使用。例如如果将新窗口的第二个参数设置为mywindow ,并在原窗口的页面上设置一个超连接<a href = "test3.html" target=mywindow> ,点击超链接时,超链接将打开新的子窗口;
第三个参数是可以用来指定新窗口的width 和 height 属性。
var newWindow;
newWindow = window.open("test.html", "mywindow", "width=200 height=200");
点击图片出现子窗口:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var newWindow;
function details(winURL){
/*这里新打开的窗口均为mywindow ,则点击第二个图片时,仍旧在原来的子窗口,而不是同时打开两个*/
newWindow = window.open(winURL,"mywindow","width=220,height=200");
/*打开的新窗口在原来窗口前面*/
newWindow.focus();
return false;
}
</script>
</head>
<body>
<h2>ONLINE BOOK HUYER</h2>
Click any of the images below for more details
<p><strong>Professional Active Server Pages .Net </strong></p>
<img src="1.gif" οnclick="details('a.html')"/>
<p><strong>Beginning Dreamweaver MX 2004 </strong></p>
<img src="1.gif" οnclick="details('b.html')"/>
</body>
在新窗口中添加HTML
第一个参数是空字符:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function details(winURL){
var newWindow = window.open("", "my", "width=120,height=120");
/*打开document对象以接受HTML输入*/
newWindow.document.open();
/*用document.write的方法写入*/
newWindow.document.write("<p>hello</p>");
newWindow.document.write("<p>welcom to here</p>");
/*关闭文档输入,若再用document.write写,会替换掉原来的HTML*/
newWindow.document.close();
/*再写入,只有new html 了*/
newWindow.document.write("<p>new HTML</p>");
}
</script>
</head>
<body>
<input type="button" value="click" οnclick="details()"/>
</body>
</html>
源窗口与新窗口数据互传
源页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
var windows;//这里 windows 必须是全局变量,否则change()函数不可以使用
function newWindow(){
windows= window.open("new.html", "new", "width=200,height=200");
/*将子窗口位置移动到距左边200像素,上边200像素*/
windows.moveTo(400, 200)
}
function change(){
/*获取新窗口的表单值*/
document.form1.text1.value=windows.document.form1.text1.value;
}
</script>
<body>
</body>
<form name="form1">
<input type="button" value="newWindow" οnclick="newWindow()"><br>
<input type="text" name="text1" value="">
<input type="button" value="change" οnclick="change()">
</form>
</html>
新页面,即作为原页面子窗口的页面new.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
var windows;//这里 windows 必须是全局变量,否则change()函数不可以使用
function newWindow(){
windows= window.open("new.html", "new", "width=200,height=200");
}
function change(){
/*获取新窗口的表单值*/
document.form1.text1.value=windows.document.form1.text1.value;
}
</script>
<body>
</body>
<form name="form1">
<input type="button" value="newWindow" οnclick="newWindow()"><br>
<input type="text" name="text1" value="">
<input type="button" value="change" οnclick="change()">
</form>
</html>
一个可以简单的添加删除数目的网页:
主窗口:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
/*添加删除的函数都是在源窗口的代码中实现的,在子页面跳转函数只需要通过 window.opener 引用源窗口即可*/
/*定义数组,书的名称以及价格,是否被添加的标识符*/
var myarray = new Array();
/*注意一维数组要变成二维数组必须要重新 new 一次*/
myarray[101] = new Array();
myarray[201] = new Array();
myarray[101][0]="Professional Active Server Pages .Net ";
myarray[101][1]="$35";
myarray[101][2]=0;
myarray[201][0]="Beginning Dreamweaver MX 2004 ";
myarray[201][1]="$35";
myarray[201][2]=0;
/*点击某本书,弹出书的详细信息以及是否添加到购物车*/
var newWindow;
function details(winURL){
newWindow = window.open(winURL,"mywindow","width=220,height=200");
newWindow.focus();
return false;
}
/*点击查询购物车,新建窗口,写入所添加的书目*/
var allwindow;
function allw(){
allwindow = window.open("c.html", "allwindow","width=220,height=200");
allwindow.document.open();
allwindow.document.write("<h4>"+"Your shopping basket contains :"+"</h4>"+"<br>");
/*basketItems 未赋值,是undefined啊,?*/
var basketItems;
/*检查购物车内有没有书的标识符*/
var contains=false;
for (basketItems in myarray ){
/*数组中的标志符,因为addBook() 中设置某书的标识符为1 */
if(myarray[basketItems][2]>0){
allwindow.document.write(myarray[basketItems][0] + "at" +myarray[basketItems][1]);
allwindow.document.write(" ");
allwindow.document.write("<input type='button'"+" οnclick='" + "window.opener.removeBook(" + basketItems + ")'"+"value='删除'"+" >");
/*购物车内有书,则设置标识符为true*/
contains=true; }
}
/*没有书*/
if(contains==false){
allwindow.document.write("<strong>you have not choose books</strong>");
}
allwindow.document.close();
}
/*添加书,添加成功后将书的detail页面关闭*/
function addBook(num){
myarray[num][2]=1;
alert("添加成功");
newWindow.close();
}
/*删除书*/
function removeBook(num){
myarray[num][2]=0;
alert("删除成功");
allwindow.close();
}
</script>
</head>
<body>
<h2>ONLINE BOOK HUYER</h2>
<input type="button" οnclick="allw()" value="查询购物车"><br>
Click any of the images below for more details
<p><strong>Professional Active Server Pages .Net </strong></p>
<img src="1.gif" οnclick="details('a.html')"/>
<p><strong>Beginning Dreamweaver MX 2004 </strong></p>
<img src="1.gif" οnclick="details('b.html')"/>
</body>
</html>
第一本书(与第二本类似):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
</script>
</head>
<body>
Professional Active Server Pages .Net
<input type="button" value="add" οnclick="window.opener.addBook(101)">
</body>
</html>
查询购物车即为空网页