如何實(shí)現(xiàn)輸入為空時(shí)跳轉(zhuǎn)默認(rèn)網(wǎng)頁?我代碼prompt里的取消為何會跳轉(zhuǎn)到其他網(wǎng)頁?
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>? ?
? <script type="text/javascript"> ?
?? ?
??? // 新窗口打開時(shí)彈出確認(rèn)框,是否打開
??? function openWindow(){
??????? var openw=confirm("是否打開窗口?");
??????? if(openw==true){
??????????? var weburl=prompt("請輸入網(wǎng)址:","http://idcbgp.cn/");
??????????? if(weburl!=null){
??????????????? openw=window.open(weburl,'_blank','width=400,height=500,menubar=no,scrollbars=no');
??????????? }
??????????? else{
??????????????? openw=window.open('http://idcbgp.cn/','_blank','width=400,height=500,menubar=no,scrollbars=no');
??????????? }
??????? }
??????? else{
??????????? alert("ByeBye!");
??????? }
??? }
??? // 通過輸入對話框,確定打開的網(wǎng)址,默認(rèn)為 http://idcbgp.cn/
??? //打開的窗口要求,寬400像素,高500像素,無菜單欄、無工具欄。
?? ?
?? ?
? </script>
?</head>
?<body>
?? ?? <input type="button" value="新窗口打開網(wǎng)站" onclick="openWindow()" />
?</body>
</html>
2018-08-17
首先,因?yàn)槟膒rompt第二參數(shù)已存在路徑,所以即使您不輸入,它也會默認(rèn)輸入已存在,其次當(dāng)取消時(shí),返回值為null,代碼將直接跳轉(zhuǎn)到此行
,所以將會跳轉(zhuǎn)到您輸入的其他網(wǎng)頁。
2018-08-16
var val=prompt();
if(val==null)
window.open();
2018-08-16
輸入空的時(shí)候返回的不是null,點(diǎn)取消返回的才是null