為什么在prompt窗口點了取消還是會打開新窗口的
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");
????????}
????}請大家指點一下,謝謝
2020-10-20
你第一個代碼段沒有問題,可以按照預期運行;:如果你把
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.xianlaiwan.cn/ ????????var?link?=?prompt("請輸入網址:",?"http://www.xianlaiwan.cn"); ????????//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>2020-10-20
重寫了第一段代碼:
<!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:","http://www.xianlaiwan.cn"); ????????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:","http://www.xianlaiwan.cn"); ????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>2020-10-19
prompt的返回值是字符串或者null。所以第三行應該是prompt(link!=null)