js post方式打开新窗口

本文介绍了一种使用JavaScript通过POST方式打开新窗口并传递参数的方法,以解决GET请求长度限制导致的数据丢失问题,适用于IE浏览器等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近想序列化表单,然后把表单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);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值