模态窗口对字符串参数的巨无聊限制

    由于IE在对象及对象引用的回收上有不少的问题,我一般很忌讳在不同的窗口和Frame之间传递脚本对象。一般情况下我都是用简单类型来传递参数,比如数字和字符串。对于复杂对象先序列化字符串再在窗口和Frame之间传递,一直以来都挺好的,也没有什么遇到什么问题。

    前几天Tester Team发现一个非常诡异的bug,在模态窗口里的显示的一些数据,当数据量大了后就会出现不确定的错误。开始以为是程序逻辑上的问题,后来调试了半天,发现原来是Modal Dialog的一个特性搞出来的。对于showModalDialog,其参数列表为:

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.

    MSDN的下面还有一个让人巨郁闷的remark:(
    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.

    这下知道了,原来是字符串参数太长,被IE自动截断了

    问题是清楚了,可是微软对模态窗口的这个限制是不是太无聊了点啊?不知道微软在搞什么飞机。

    解决这个问题,如果按MSDN说的,我们把vArguments弄成object或array就行了。那么对于参数传出和传入的地方都需要修改"接口"的代码,好麻烦的说。那么能不能让字符串参数突破vArguments的4096个字符的限制呢?

    其实对于一个字符串,只要我们把它包装(有点像C#的Boxing了)成string object,就可以不受vArguments长度的限制了,而且在Modal Dialog中处理字符串的接口函数不需要做任何的修改。比如我们要传递一个5001个字符的字符串,示例如下:
<button onclick="pop()">pop</button>
<script language="javascript">
function pop()
{
    
var str = '';
    for ( var i=0 ; i < 5000 ; ++i )
    
{
        str 
+= 'a';
    }

    
// alert(str.length);
    window.showModalDialog('pop.htm', new String(str+'b'));
}

</script>

    就这样使用一个简单的 new String(str+'b') 就完全突破限制了

posted on 2005-08-27 16:58  birdshome  阅读(5474)  评论(15编辑  收藏  举报

导航