window.open() post請求傳參數

windowOpen.js插件

jQuery.fn.openWindow = function (urlParam, type){
		//獲取url
		var url=urlParam.slice(0,urlParam.indexOf('?') );
		//獲取參數
		var param = urlParam.slice(urlParam.indexOf('?') + 1).split('&');
		//創建form表單
		var tempForm = document.createElement("form");
	    tempForm.id = "tempForm1";
	    tempForm.method = "post";
	    tempForm.action = url;
	    tempForm.target = type;    // _blank - URL加載到一個新的窗口
	   
		//循環參數創建input
	    var hash;
	    for (var i = 0; i < param.length; i++) {
	        hash = param[i].split('=');
	        var hideInput = document.createElement("input");
	        hideInput.type = "hidden";
	        hideInput.name = hash[0];
	        hideInput.value = hash[1];
	        tempForm.appendChild(hideInput);
	    }
	    if(document.all){    // 兼容不同瀏覽器
	        tempForm.attachEvent("onsubmit",function(){});        //IE
	    }else{
	        tempForm.addEventListener("submit",function(){},false);    //firefox
	    }
	    document.body.appendChild(tempForm);
	    if(document.all){    // 兼容不同瀏覽器
	        tempForm.fireEvent("onsubmit");
	    }else{
	        tempForm.dispatchEvent(new Event("submit"));
	    }
	    tempForm.submit();
	    document.body.removeChild(tempForm);
	}

使用方法

//仍然使用url傳參的形式,實際使用post傳參數
//實際瀏覽器的地址爲www.baidu.com
var url='www.baidu.com?projectId=1&stage=2'
//第二個參數:
//_blank - URL加載到一個新的窗口。這是默認
//_parent - URL加載到父框架
//_self - URL替換當前頁面
//_top - URL替換任何可加載的框架集
$().openWindow(url,"_self");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章