function openWindow(){
var clickConfirm = confirm("是否打开网址输入框?");
if (clickConfirm == true){
var link = prompt("Please input the website address:","https://qq.com");
window.open(link,'_blank','width=500,height=600');
}
else {
alert("helloworld");
}
}我想实现点击确认打开新窗口,点击取消弹alert,但是好像不行。
另外我想尝试:
1、点击按钮先弹prompt窗口;点击确认再弹出confirm窗口;点击取消弹alert
2、弹出confirm窗口,点击确认打开新网页窗口,点击取消弹alert
试了很多个方式,但是都会提示else错误等等,无法正常实现我需要的功能。比如如下代码:
function openWindow(){
var link = prompt("Please input the website address:","https://qq.com");
if (prompt == true){
var clickConfirm = confirm("是否打开网址输入框?");
if (clickConfirm == true){
window.open(link,'_blank','width=500,height=600');
}
else {
alert("helloworld");
}
}
else {
alert("helloworld");
}
}请大家指点一下,谢谢
你第一个代码段没有问题,可以按照预期运行;:如果你把if (clickConfirm == true)误打成if (clickConfirm = true)就会出现你描述的问题,这段代码和你测试时的代码是否不同?你第二个代码段错在对prompt方法返回值的理解。下面的代码是对你的描述的一种实现:
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<script type="text/javascript">
function openWindow(){
// 通过prompt方法,输入要打开的网址,默认为 http://www.imooc.com/
var link = prompt("请输入网址:", "http://www.imooc.com");
//prompt方法点击取消返回null
if(link == null){
alert("prompt方法点击取消");
}
//prompt方法点击确认返回字符串,(包括空字符串"")
else{
//弹出确认框,确认是否打开网址
var confirmMessage = confirm("确认打开网址?")
//confirm方法点击确认返回布尔值true,点击取消返回布尔值false
if(confirmMessage){
//打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏。
window.open(link, "_blank", "width=500, height=600");
}
else{
alert("confirm方法点击取消");
}
}
}
</script>
</head>
<body>
<input type="button" value="新窗口打开网站" onclick="openWindow()" />
</body>
</html>重写了第一段代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>alert</title>
<script type="text/javascript">
function rec(){
var confirmMessage = confirm("do you want to open the address dialog?");
if (confirmMessage){
var link = prompt("please input the link:","https://www.imooc.com");
if (link == null){
alert("You are cancel the prompt dialog!");
}
else {
window.open(link,"_blank","width=500,height=600");
}
}
else {
alert("You are cancel the confirm dialog!");
}
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,弹出对话框" />
</body>
</html>根据楼上热心朋友的解答,写了第二段:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>alert</title>
<script type="text/javascript">
function rec(){
var inputLink = prompt("please input your link:","https://www.imooc.com");
if (inputLink !== null){
var confirmMessage = confirm("Do you confirm to open this link?");
if(confirmMessage){
window.open(inputLink,"_blank","width=500, height=500");
}
else {
alert("You are cancel to open this link!");
}
}
else {
alert("You are cancel to open the link directly!")
}
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,弹出对话框" />
</body>
</html>prompt的返回值是字符串或者null。所以第三行应该是prompt(link!=null)