1. 程式人生 > >js彈出框、對話方塊、提示框、彈窗總結

js彈出框、對話方塊、提示框、彈窗總結

一、JS的三種最常見的對話方塊

//====================== JS最常用三種彈出對話方塊 ========================

    //彈出對話方塊並輸出一段提示資訊
    function ale() {
        //彈出一個對話方塊
        alert("提示資訊!");

    }

    //彈出一個詢問框,有確定和取消按鈕
    function firm() {
        //利用對話方塊返回的值 (true 或者 false)
        if (confirm("你確定提交嗎?")) {
            alert("點選了確定");
        }
        else {
            alert("點選了取消");
        }

    }

    //彈出一個輸入框,輸入一段文字,可以提交
    function prom() {
        var name = prompt("請輸入您的名字", ""); //將輸入的內容賦給變數 name ,

        //這裡需要注意的是,prompt有兩個引數,前面是提示的話,後面是當對話框出來後,在對話方塊裡的預設值
        if (name)//如果返回的有內容
        {
            alert("歡迎您:" + name)
        }

    }

二、點選按鈕時常用的6中提示框和操作
<!-----------按鈕提示框---------->
<input type="button" name="btn2" id="btn2" value="刪除" onclick="return confirm('Yes/No'););

<!-----------按鈕提示框----------> 
<input type="button" name="btn2" id="btn2" value="提示" onclick="javaScript:alert('您確定要刪除嗎?');

<!-----------提交按鈕----------> 
<input type="button" value="提交" onclick="javaScript:window.location.href='http://www.baidu.com';"/>

<!-----------關閉按鈕----------> 
<input type="button" value="關閉" onclick="javaScript:window.close();">

<!-----------返回並關閉連線----------> 
<a href="#" onclick="javascript:;window.opener.location.reload();window.close()">返回</a>
javaScript:window.location.reload();//返回當前頁並重新整理

<!-----------返回上一級頁面----------> 
<input type="button" name="button" value="< 返回" onclick="javascript:history.go(-1)"/>

三、彈出獨立視窗
//關閉,父視窗彈出對話方塊,子視窗直接關閉 
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+"';");


<SCRIPT LANGUAGE="javascript"> 
<!-- 
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為允許;

'newwin':隱藏選單欄位址列工具條 

四、彈出視窗例項演示

    //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!

參考文章:http://blog.csdn.net/xiaotaoqibao/article/details/6797089