您的位置:首页 > Web前端 > JavaScript

js post方式打开新窗口

2015-11-17 11:42 513 查看
最近想序列化表单,然后把表单json字符串传到后台实现不保存的预览功能,但是苦于在window.open()是get方式请求,有长度显示,当长度过长就会出现丢失部分json字符串问题,在火狐没问题,但是在ie就有这个情况,所以找了js post方式 打开窗口提交的方法。

如下所示:

1.js函数

/**

         * post 方式打开新窗口

         * url:请求地址

         * name:窗口名

         * width:宽度

         * height:高度

         * resizable:是否可调整大小

         * keys:参数名数组集合

         * values:参数值数组集合

         */

        function openWindowWithPost(url, name, width,height,resizable,keys, values) {

            var screenWidth = screen.availWidth,screenHeight = screen.availHeight;

            var para = "";

            para += 'width=' + (width ? width : screenWidth-20);

            para += ',height=' + (height ? height : screenHeight-43);

            para += ',left=' + (width ? (screenWidth-width)/2 : 0);

            para += ',top=' + (height ? (screenHeight-height)/2 : 0);

            if(resizable) para += ',resizable = yes';

            if(!name) name = "";

            

            var newWindow=window.open("",name,para);

            if (!newWindow){

                return false;

            }

            

            var html = "";

            html += "<html><head></head><body><form id='formid' method='post' action='"    + url + "'>";

            if (keys && values && (keys.length == values.length)){

                for ( var i = 0; i < keys.length; i++){

                    html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";

                }

            }

            

            html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()<\/script><\/body><\/html>";

            newWindow.document.write(html);

            return newWindow;

        }

    </script>

2.调用

            var url = "xxxx";

            var keys=[];

            keys[0] = "id";

            var values=[];

            values[0] = document.getElementById("id").value;

            openWindowWithPost(url, "预览",500,400,null, null, null);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: