用window.showModalDialog傳遞任意長度的參數

    在作項目時,也許需要把大量數據傳到在本頁打開的界面.如用window.open或window.showModalDialog. 如果想傳送大量數據比如超過3000字符. 一般會因爲傳輸長度的限制只能傳部分過去.
    如果要實現這個功能的話, 可以用window.showModalDialog. 這個方法可以傳送對象類型的參數. 我們把要傳遞的值賦給這個對象, 然後傳遞這個對象.這樣就可以實現任意數據長度的傳輸了.
    實例如下:
    Noname1.html
   

    
<html>
    
<head>
    
<script language="javascript">
        
function selectPic(){
        
            
var sFeatures = "status:no;dialogHeight:500px;dialogwidth:500px;scroll=no;";
            
var obj = new Object();
            
var tmpVal="";
            obj.name
="51js";
            tmpVal 
= document.all("22").value;
            
if(tmpVal == "") tmpVal="aaaaaaaaaaaaaaaaaaaaa";
            
while(tmpVal.length<50000){
                tmpVal 
= tmpVal + tmpVal;
            }

            alert(
"傳輸長度=" + tmpVal.length);
            obj.id
= tmpVal;
            
            window.showModalDialog(
"Noname2.html",obj,sFeatures);
                    
//window.open("Noname2.html","aaaa","height=500,width=500,top=100,left=100,status=no,toolbar=no,menubar=no,location=no");

        }

    
</script>
    
<title> New Document </title>
    
<meta name="Author" content="">
    
<meta name="Keywords" content="">
    
<meta name="Description" content="">
    
</head>
    
<body>
        
<input id='11' type="image" name="upload" value="hi" src="D:BFPWorkSpaceBFPClientwebimages.gif" onclick="selectPic()">
        
<input id='22' type='text' value=''>
    
</body>
    
</html>

 Noname2.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 getMessage(){
    
var obj = window.dialogArguments;
    
    document.all(
"ww").value = obj.id;
    alert(
"接受長度=" + document.all("ww").value.length)

}

</script>
</head>

<body style="backcolor='blue'" onload='getMessage()'>
<form>
   
<input type="file" name="uploadFile" value="">
   
<input type="submit" name="submit" value="submit">
   
<br>
    
<TEXTAREA rows=20 cols="100" ID='ww'>
    
</TEXTAREA>


</form>
</body>
</html>

解釋一下: 在Noname1.html中, 需要在text框中輸入幾個字符或不輸. 在Noname1.html的js中, 我把字符的長度變成超過50000個. 然後傳輸. 這樣方便實現了大數量傳輸的目的.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章