javascript入门经典---笔记6 (打开新窗口)

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>
查询购物车即为空网页




Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值