1. 程式人生 > >JS 以POST方式開啟新頁面

JS 以POST方式開啟新頁面

/*
        *功能: JS跳轉頁面,並已POST方式提交資料
        *引數: URL 跳轉地址 PARAMTERS 引數
        */
        function ShowReport_Click() {

            var parames = new Array();
            parames.push({ name: "name", value: "張三"});
            parames.push({ name: "code", value: "123456"});

            Post("www.baidu.com", parames);

            return false;
        }

        /*
        *功能: 模擬form表單的提交
        *引數: URL 跳轉地址 PARAMTERS 引數
        */
        function Post(URL, PARAMTERS) {
            //建立form表單
            var temp_form = document.createElement("form");
            temp_form.action = URL;
            //如需開啟新視窗,form的target屬性要設定為'_blank'
            temp_form.target = "_self";
            temp_form.method = "post";
            temp_form.style.display = "none";
            //新增引數
            for (var item in PARAMTERS) {
                var opt = document.createElement("textarea");
                opt.name = PARAMTERS[item].name;
                opt.value = PARAMTERS[item].value;
                temp_form.appendChild(opt);
            }
            document.body.appendChild(temp_form);
            //提交資料
            temp_form.submit();
        }