標籤:
一、JS最常用三種彈出對話方塊
1、對話方塊
//彈出對話方塊並輸出一段提示資訊 function ale() { //彈出一個對話方塊 alert("提示資訊!"); }
2、詢問框
//彈出一個詢問框,有確定和取消按鈕 function firm() { //利用對話方塊返回的值 (true 或者 false) if (confirm("你確定提交嗎?")) { alert("點擊了確定"); } else { alert("點擊了取消"); } }
3、輸入框
//彈出一個輸入框,輸入一段文字,可以提交 function prom() { var name = prompt("請輸入您的名字", ""); //將輸入的內容賦給變數 name , //這裡需要注意的是,prompt有兩個參數,前面是提示的話,後面是當對話方塊出來後,在對話方塊裡的預設值 if (name)//如果返回的有內容 { alert("歡迎您:" + name) } }
4、綜合小案例
<script>var isflag=confirm("勇士,準備好營救公主了嘛?");if(isflag){var mytype=prompt("哪兒請問勇士叫什麼名字");alert(mytype);alert("開始遊戲");}else{alert("快滾快滾");}</script>
二、彈出獨立視窗
window.open 彈出新視窗的命令;
‘page.html‘ 快顯視窗的檔案名稱;
‘newwindow‘ 快顯視窗的名字(不是檔案名稱),非必須,可用空‘‘代替;
height=100 視窗高度;
width=400 視窗寬度;
top=0 視窗距離螢幕上方的象素值;
left=0 視窗距離螢幕左側的象素值;
toolbar=no 是否顯示工具列,yes為顯示;
menubar,scrollbars 表示功能表列和滾動欄。
resizable=no 是否允許改變視窗大小,yes為允許;
location=no 是否顯示地址欄,yes為允許;
status=no 是否顯示狀態列內的資訊(通常是檔案已經開啟),yes為允許;
‘newwin‘:隱藏功能表列地址欄工具條
//關閉,父視窗彈出對話方塊,子視窗直接關閉 this.Response.Write("<script language=javascript>window.close();</script>"); //關閉,父視窗和子視窗都不彈出對話方塊,直接關閉 this.Response.Write("<script>"); this.Response.Write("{top.opener =null;top.close();}"); this.Response.Write("</script>"); //快顯視窗重新整理當前頁面width=200 height=200菜單。功能表列,工具條,地址欄,狀態列全沒有 this.Response.Write("<script language=javascript>window.open(‘rows.aspx‘,‘newwindow‘,‘width=200,height=200‘)</script>"); //快顯視窗重新整理當前頁面 this.Response.Write("<script language=javascript>window.open(‘rows.aspx‘)</script>"); this.Response.Write("<script>window.open(‘WebForm2.aspx‘,‘_blank‘);</script>"); //彈出提示視窗跳到webform2.aspx頁(在一個IE視窗中) this.Response.Write(" <script language=javascript>alert(‘註冊成功‘);window.window.location.href=‘WebForm2.aspx‘;</script> "); //關閉當前子視窗,重新整理父視窗 this.Response.Write("<script>window.opener.location.href=window.opener.location.href;window.close();</script>"); this.Response.Write("<script>window.opener.location.replace(window.opener.document.referrer);window.close();</script>"); //子視窗重新整理父視窗 this.Response.Write("<script>window.opener.location.href=window.opener.location.href;</script>"); this.Response.Write("<script>window.opener.location.href=‘WebForm1.aspx‘;</script>"); //彈出提示視窗.確定後彈出子視窗(WebForm2.aspx) this.Response.Write("<script language=‘javascript‘>alert(‘發表成功!‘);window.open(‘WebForm2.aspx‘)</script>"); //彈出提示視窗,確定後,重新整理父視窗 this.Response.Write("<script>alert(‘發表成功!‘);window.opener.location.href=window.opener.location.href;</script>"); //彈出相同的一頁 <input type="button" value="Button" onclick="javascript:window.open(window.location.href)"> // Response.Write("parent.mainFrameBottom.location.href=‘yourwebform.aspx?temp=" +str+"‘;"); <!-- window.open (‘page.html‘, ‘newwindow‘, ‘height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no‘)
//這句要寫成一行 -->
三、快顯視窗執行個體示範
//1、最基本的快顯視窗代碼 window.open (‘page.html‘) //2、經過設定後的快顯視窗 window.open(‘page.html‘, ‘newwindow‘, ‘height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no‘) //這句要寫成一行 //參數解釋: //window.open 彈出新視窗的命令; //‘page.html‘ 快顯視窗的檔案名稱; //‘newwindow‘ 快顯視窗的名字(不是檔案名稱),非必須,可用空‘‘代替; //height=100 視窗高度; //width=400 視窗寬度; //top=0 視窗距離螢幕上方的象素值; //left=0 視窗距離螢幕左側的象素值; //toolbar=no 是否顯示工具列,yes為顯示; //menubar,scrollbars 表示功能表列和滾動欄。 //resizable=no 是否允許改變視窗大小,yes為允許; //location=no 是否顯示地址欄,yes為允許; //status=no 是否顯示狀態列內的資訊(通常是檔案已經開啟),yes為允許; //3、用函數控制快顯視窗 function openwin() { window.open("page.html", "newwindow", "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") //寫成一行 } $(document).ready(fucntion(){ openwin(); }); //這裡定義了一個函數openwin(),函數內容就是開啟一個視窗。在調用它之前沒有任何用途。怎麼調用呢? //方法一:<body onload="openwin()"> 瀏覽器讀頁面時快顯視窗; //方法二:<body onunload="openwin()"> 瀏覽器離開頁面時快顯視窗; //方法三:用一個串連調用: //<a href="#" onclick="openwin()">開啟一個視窗</a> //注意:使用的“#”是虛串連。 //方法四:用一個按鈕調用: //<input type="button" onclick="openwin()" value="開啟視窗"> //4、同時彈出2個視窗 function openwin() { window.open("page.html", "newwindow", "height=100, width=100, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=n o, status=no")//寫成一行 window.open("page2.html", "newwindow2", "height=100, width=100, top=1 00, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, loca tion=no, status=no")//寫成一行 } //為避免彈出的2個視窗覆蓋,用top和left控制一下彈出的位置不要相互覆蓋即可 。最後用上面說過的四種方法調用即可。 //注意:2個視窗的name(newwindows和newwindow2)不要相同,或者乾脆全部為空白。 //5、主視窗開啟檔案1.htm,同時彈出小視窗page.html function openwin() { window.open("page.html", "", "width=200,height=200") } //調用:<a href="1.htm" onclick="openwin()">open</a> //6、彈出的視窗之定時關閉控制 //下面我們再對彈出的視窗進行一些控制,效果就更好了。如果我們再將一小段 代碼加入彈出的頁面(注意是加入page.html的HTML中,不是首頁面中),讓它10秒後自動關閉是不是更酷了? //首先,將如下代碼加入page.html檔案的<head>區: function closeit() { setTimeout("self.close()", 10000) //毫秒 } //頁面載入完成調用關閉事件 $(document).ready(fucntion(){ closeit(); }); //7、在快顯視窗中加上一個關閉按鈕 //<INPUT TYPE=‘BUTTON‘ VALUE=‘關閉‘ onClick=‘window.close()‘> //8、內包含的快顯視窗-一個頁面兩個視窗 //上面的例子都包含兩個視窗,一個是主視窗,另一個是彈出的小視窗。通過下面的例子,你可以在一個頁面內完成上面的效果。 function openwin() { OpenWindow = window.open("", "newwin", "height=250, width=250,toolbar=no ,scrollbars=" + scroll + ",menubar=no"); //寫成一行 OpenWindow.document.write("<TITLE>例子</TITLE>") OpenWindow.document.write("<BODY BGCOLOR=#ffffff>") OpenWindow.document.write("<h1>Hello!</h1>") OpenWindow.document.write("New window opened!") OpenWindow.document.write("</BODY>") OpenWindow.document.write("</HTML>") OpenWindow.document.close() } //<a href="#" onclick="openwin()">開啟一個視窗</a> //<input type="button" onclick="openwin()" value="開啟視窗"> //9、終極應用--彈出的視窗之Cookie控制 //回想一下,上面的快顯視窗雖然酷,但是有一點小毛病,比如你將上面的指令碼放在一個需要頻繁經過的頁面裡(例如首頁),那麼每次重新整理這個頁面,視窗都會彈出一次,我們使用cookie來控制一下就可以了。 //首先,將如下代碼加入首頁面HTML的<HEAD>區: function openwin() { window.open("page.html", "", "width=200,height=200") } function get_cookie(Name) { var search = Name + "=" var returnvalue = ""; if (document.cookie.length > 0) { offset = document.cookie.indexOf(search) if (offset != -1) { offset += search.length end = document.cookie.indexOf(";", offset); if (end == -1) end = document.cookie.length; returnvalue = unescape(document.cookie.substring(offset, end)) } } return returnvalue; } function loadpopup() { if (get_cookie(‘popped‘) == ‘‘) { openwin() document.cookie = "popped=yes" } } //然後,用<body onload="loadpopup()">(注意不是openwin而是loadpop啊!)替換首頁面中原有的<BODY>這一句即可。
你可以試著重新整理一下這個頁面或重新進入該頁面,視窗再也不會彈出了。真正的Pop-Only-Once!
js-快顯視窗