使用javascript開啟模態對話方塊

來源:互聯網
上載者:User

1. 標準的方法

<script type="text/javascript">  
function openWin(src, width, height, showScroll){  
window.showModalDialog (src,"","location:No;status:No;help:No;dialogWidth:"+width+";dialogHeight:"+height+";scroll:"+showScroll+";");  
}  
</script> 
例:<span style="CURSOR: pointer" onclick="openWin 
(’http://www.deepteach.com’, ’500px’, ’400px’, ’no’)">點擊</span> 

 

2. 要注意的是,Firefox並不支援該功能,它支援的文法是

window.open 
(’openwin.html’,'newWin’, 'modal=yes, width=200,height=200,resizable=no, scrollbars=no’ ); 

 

3. 如何自動判斷瀏覽器

<input type="button" value="開啟對話方塊" onclick="showDialog('#')"/> 
  <SCRIPT   LANGUAGE="JavaScript"> 
  <!-- 
  function   showDialog(url) 
  { 
   if(   document.all   ) //IE 
   { 
   feature="dialogWidth:300px;dialogHeight:200px;status:no;help:no"; 
   window.showModalDialog(url,null,feature); 
   } 
   else 
   { 
     //modelessDialog可以將modal換成dialog=yes 
   feature ="width=300,height=200,menubar=no,toolbar=no,location=no,"; 
   feature+="scrollbars=no,status=no,modal=yes";   
   window.open(url,null,feature); 
   } 
  } 
  //--> 
</SCRIPT>

 

4. 在IE中,模態對話方塊會隱藏地址欄,而在其他瀏覽器則不一定

 

【注意】在Google瀏覽器中,這個模態的效果也會失效。

 

 

5. 一般在彈出對話方塊的時候,我們都希望整個父頁面的背景變為一個半透明的顏色,讓使用者看到後面是不可以訪問的

而關閉對話方塊之後又希望還原

 

這是怎麼做到的呢?

        ///顯示某個訂單的詳細資料,通過一個模態對話方塊,而且螢幕會變顏色
        function ShowOrderDetails(orderId) {
            var url = "details.aspx?orderID=" + orderId;

//            $("body").css("filter", "Alpha(Opacity=20)");
            //filter:Alpha(Opacity=50)

            $("body").addClass("body1");

            ShowDetailsDialog(url, "600px", "400px", "yes");

            $("body").removeClass("body1");
        }

 

另外,有一個樣式表定義

.body1
{
    background-color:#999999;
    filter:Alpha(Opacity=40);
}

 

6. 如何在頁面之間傳遞數值

下面的部分,轉載自:http://www.cnblogs.com/tohen/archive/2007/12/15/995868.html

(一)showModalDialog使用例子,父視窗向子視窗傳遞值,子視窗設定父視窗的值,子視窗關閉的時候傳回值到父視窗.

farther.html
---------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META content="EditPlus" name="Generator">
<META content="" name="Author">
<META content="" name="Keywords">
<META content="" name="Description">
<script language="javascript">
<!--
function openChild(){
var k = window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px");
if(k != null)
        document.getElementById("txt11").value = k;
        }
//-->
</script>
</HEAD>
<BODY>
<FONT face="宋體"></FONT>
<br>
        傳遞到父視窗的值:<input id="txt9" type="text" value="3333333333333" name="txt9"><br>
        返回的值:<input id="txt11" type="text" name="txt11"><br>
        子視窗設定的值:<input id="txt10" type="text" name="txt10"><br>
<input id="Button1" onclick="openChild()" type="button" value="openChild" name="Button1">
</BODY>
</HTML>

child.html
---------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META content="EditPlus" name="Generator">
<META content="" name="Author">
<META content="" name="Keywords">
<META content="" name="Description">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
</HEAD>
<BODY>
<FONT face="宋體"></FONT>
<br>
        父視窗傳遞來的值:<input id="txt0" type="text" name="txt0"><br>
        輸入要設定父視窗的值:<input id="txt1" type="text" name="txt1"><input id="Button1" onclick="setFather()" type="button" value="設定父視窗的值" name="Button1"><br>
        輸入返回的值:<input id="txt2" type="text" name="txt2"><input id="Button2" onclick="retrunValue()" type="button" value="關閉切傳回值" name="Button2">
<input id="Button3" onclick="" type="button" value="關閉重新整理父視窗" name="Button3">
<script language="javascript">
<!--
var k=window.dialogArguments;
//獲得父視窗傳遞來的值
if(k!=null)
        {
        document.getElementById("txt0").value = k.document.getElementById("txt9").value;
        }
//設定父視窗的值
function setFather()
        {
        k.document.getElementById("txt10").value = document.getElementById("txt1").value
        }
//設定返回到父視窗的值
function retrunValue()
        {
var s = document.getElementById("txt2").value;
        window.returnValue=s;
        window.close();
        }
//-->
</script>
</BODY>
</HTML>

----------------------------
說明:
由於showModalDialog緩衝嚴重,下面是在子視窗取消用戶端緩衝的設定.也可以在伺服器端取消緩衝,參考:
http://adandelion.cnblogs.com/articles/252137.html
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
(二)下面是關閉重新整理父視窗的例子

farther.html
---------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="javascript">
<!--
function openChild()
        {
var k = window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px");
if(k == 1)//判斷是否重新整理
        {
        alert('重新整理');
        window.location.reload();
        }
        }
//-->
</script>
</HEAD>
<BODY>
<br>
        傳遞到父視窗的值:<input id="txt9" type="text" value="3333333333333" NAME="txt9"><br>
<input type="button" value="openChild" onclick="openChild()" ID="Button1" NAME="Button1">
</BODY>
</HTML>

child.html
----------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document </TITLE>
<META content="EditPlus" name="Generator">
<META content="" name="Author">
<META content="" name="Keywords">
<META content="" name="Description">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
</HEAD>
<BODY>
<FONT face="宋體"></FONT>
<br>
        父視窗傳遞來的值:<input id="txt0" type="text" name="txt0"><br>
<input id="Button1" onclick="winClose(1)" type="button" value="關閉重新整理父視窗" name="Button1">
<input id="Button2" onclick="winClose(0)" type="button" value="關閉不重新整理父視窗" name="Button2">
<script language="javascript">
<!--
var k=window.dialogArguments;
//獲得父視窗傳遞來的值
if(k!=null)
        {
        document.getElementById("txt0").value = k.document.getElementById("txt9").value;
        }
//關閉視窗返回是否重新整理的參數.
function winClose(isRefrash)
        {
        window.returnValue=isRefrash;
        window.close();
        }
//-->
</script>
</BODY>
</HTML>

--------------------------
說明
1.下面是取消用戶端緩衝的:
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
也可以在伺服器端取消緩衝,參考:
http://adandelion.cnblogs.com/articles/252137.html

2.向父視窗傳遞闡述在ASP.NET中也可以是用aaa.aspx?id=1的方式傳遞.

3.不重新整理父視窗的話在父視窗中直接這樣一來設定可以.
<script>
window.showModalDialog("child.html",window,"dialogWidth:335px;status:no;dialogHeight:300px");
</script>
4.在子視窗中若要提交頁面的話要加入:,這樣就不會開啟新視窗了.
<head>
<base target="_self">
</HEAD>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.