js的showModalDialog对字符串的限制
不知道,大家有没有遇到这样的奇葩问题,当时今天笔者很不幸,遇到这一奇葩的问题,就当使用window.showModalDialog("test.jsp",args,‘dialogHeight=100px;dialogWidth=250px;‘);进行数据传输,当args的数据大于4096个字符时,多出来的数据会被自动截掉,从而导致数据的完整性丢失.
Syntax
vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
Parameters
sURL | Required. String that specifies the URL of the document to load and display. |
vArguments | Optional. Variant that specifies the arguments to use when displaying the document. Use this parameter to pass a value of any type, including an array of values. The dialog box can extract the values passed by the caller from the dialogArguments property of the window object. |
The vArguments parameter can be referenced within the modal dialog box using the dialogArguments property of the window object. If the vArguments parameter is defined as a string, the maximum string length that can be passed to the modal dialog box is 4096 characters; longer strings are truncated.
根据MSDN的说法,我们把args弄成object或array就行了,那么对于参数传出和传入的地方都需要修改"接口"的代码,这样一来未免就一些许麻烦。那么能不能让字符串参数突破args的4096个字符的限制呢?
其实对于一个字符串来说,只要我们把它包装成string object,就可以不再args长度的限制了,而且在Modal Dialog中处理字符串的接口函数不需要做任何的修改,如下代码:
function test(id){ var _bpc; jQuery.ajax({ url: 'test.html', data: {'id': id, 'name': name}, type : "POST", async: false, cache : false, dataType : "json", success : function(data) { var result = json_parse(unescape(data)); var selectHTML = []; if(result){ if(result.length == 1){ _bpc = {'id': result[0][0], 'name': result[0][1]}; } else if(result.length > 1){ selectHTML .push('<select name="">'); for(var i = 0, le = result.length; i < le; i++){ var item = result[i]; selectHTML.push('<option id="bpc' + item[0] + '" value="' + item[0] + '">' + item[1] + '</option>'); } selectHTML.push('</select>'); alert(selectHTML.length); _obj = window.showModalDialog('index.html?'+'Math.random().toString()',selectHTML, 'dialogHeight=100px;dialogWidth=250px;'); if(!_obj) _obj = {'id': -1, 'name': ''}; } } }, error: function(e){ alert('查询失败,请稍后再试!'); } }); return _obj; }数据接收页面:
<script type="text/javascript" language="javascript"> function finish(){ var sel = document.getElementById('selDiv').firstChild; if(sel){ var selectedItem = sel.options[sel.selectedIndex]; window.returnValue = {'id': selectedItem.value, 'name': selectedItem.text}; } window.close(); } window.onload =function(){ var args = window.dialogArguments; if(args != null){ var bpcsArgs = args.join(''); document.getElementById("selDiv").innerHTML = bpcsArgs; document.getElementById("selDiv").firstChild.focus(); } } </script> </head> <body style="text-align: center;background: #ECE9D8; margin: 5px auto;" onkeydown="if(event.keyCode==13)finish()"> <table align="center"> <tr align="left"><td>该档案存在于以下流水类型中</td></tr> <tr align="left"><td>请选择一个后点击确定</td></tr> <tr align="center"><td><div id="selDiv"></div></td></tr> <tr align="center"><td><input type="submit" value="确定" onclick="finish()" /> <input type="button" value="取消" onclick="window.close()" /></td></tr> </table> </body> </html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。