使用javascript打開模態對話框
1. 標準的方法
<mce: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+";"); } // --></mce:script>
例:<span >點擊</span>
2. 要注意的是,Firefox並不支持該功能,它支持的語法是
window.open
(’openwin.html’,'newWin’, 'modal=yes, width=200,height=200,resizable=no, scrollbars=no’ );
3. 如何自動判斷瀏覽器
<input type="button" value="打開對話框" /> <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. 如何在頁麵之間傳遞數值
(一)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"> <mce: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; } // --></mce:script> </HEAD> <BODY> <FONT face="宋體"></FONT> <br> 傳遞到父窗口的值:<input type="text" value="3333333333333" name="txt9"><br> 返回的值:<input type="text" name="txt11"><br> 子窗口設置的值:<input type="text" name="txt10"><br> <input 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 type="text" name="txt0"><br>
輸入要設置父窗口的值:<input type="text" name="txt1"><input type="button" value="設置父窗口的值" name="Button1"><br>
輸入返回的值:<input type="text" name="txt2"><input type="button" value="關閉切返回值" name="Button2">
<input 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緩存嚴重,下麵是在子窗口取消客戶端緩存的設置.也可以在服務器端取消緩存,參考:
https://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 type="text" value="3333333333333" NAME="txt9"><br>
<input type="button" value="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 type="text" name="txt0"><br>
<input type="button" value="關閉刷新父窗口" name="Button1">
<input 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>
最後更新:2017-04-02 04:00:24