Window. open () implements post transmission parameter, window. openpost

Source: Internet
Author: User

Window. open () implements post transmission parameter, window. openpost

In actual projects, we often encounter this requirement, that is, to jump between sub-system pages and open them on a new page. My project team uses the SSH framework, therefore, the URLs are all similar ****. action, with two parameters (System ID and system name). The two parameters are intercepted by struts and saved to the session, on the sub-system page that is opened, a tree menu implemented by the ztree plug-in requires the Parameter System ID to be initialized. The window is used directly. open (url, "_ blank") causes the url to be too long and some parameters are exposed. Therefore, we want to use the post method to commit and hide the passing of parameters in the commit process. First, I thought of ajax submission, but there will be a problem in passing the two parameters. ajax submission and window. open () will make the action go twice, so I will discard it. Then I carefully read the window. open () API again. link address: http://www.w3school.com.cn/jsref/met_win_open.asp. Window. open () is the get submission method by default. To implement the post submission method, you have to think of another method. For more information, see http://www.bkjia.com/article/32826.htm. This is also a commonly used method. I will slightly modify it based on the actual situation:

Copy codeThe Code is as follows:
Function openPostWindow (url, name, data1, data2 ){
Var tempForm = document. createElement ("form ");
TempForm. id = "tempForm1 ";
TempForm. method = "post ";
TempForm. action = url;
TempForm.tar get = name;
Var hideInput1 = document. createElement ("input ");
HideInput1.type = "hidden ";
HideInput1.name = "xtid ";
HideInput1.value = data1;
Var hideInput2 = document. createElement ("input ");
HideInput2.type = "hidden ";
HideInput2.name = "xtmc ";
HideInput2.value = data2;
TempForm. appendChild (hideInput1 );
TempForm. appendChild (hideInput2 );
If (document. all ){
TempForm. attachEvent ("onsubmit", function () {}); // IE
} Else {
Var subObj = 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 );
}
// Function openWindow (name ){
// Window. open ("", name );
//}

The number of parameters in the openPostWindow () function is modified based on actual needs. Data1 and data2 are parameters to be passed by action. In addition, Javascript event browser compatibility needs to be considered here. I have commented out function openWindow () here. Otherwise, an additional blank page (about: blank) will be opened ). This basically satisfies the needs.

The above is all the content shared in this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.