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

2024-05-11 01:48

本文主要是介绍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>
查询购物车即为空网页




这篇关于javascript入门经典---笔记6 (打开新窗口)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/978167

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满