js的showModalDialog对字符串的限制

来源:互联网 发布:webp转换jpg软件 编辑:程序博客网 时间:2024/06/02 04:29

不知道,大家有没有遇到这样的奇葩问题,当时今天笔者很不幸,遇到这一奇葩的问题,就当使用window.showModalDialog("test.jsp",args,'dialogHeight=100px;dialogWidth=250px;');进行数据传输,当args的数据大于4096个字符时,多出来的数据会被自动截掉,从而导致数据的完整性丢失.

Syntax
    vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
Parameters
sURLRequired. String that specifies the URL of the document to load and display.vArgumentsOptional. 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.MSDN给出了如下标识:
    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>




0 0