Home > Web Front-end > JS Tutorial > body text

window.open() implements post passing parameters_javascript skills

WBOY
Release: 2016-05-16 16:10:08
Original
1681 people have browsed it

In actual projects, we often encounter the need to jump between subsystem pages and open them in a new page. My project team uses the SSH framework, so the URLs are similar to **** .action, and also has two parameters (system ID and system name). The two parameters are intercepted by struts and stored in the session. In the opened subsystem page, there is also a tree menu implemented by the ztree plug-in that requires the parameter system ID. To initialize, using window.open(url, "_blank") directly will make the url length too long and expose some parameters. Therefore, I want to use the post method to submit, hiding the transfer of parameters during the submission process. First of all, I think of ajax submission, but there will be problems with the transfer of the two parameters. Ajax submission and window.open() will make the action go through twice, so they are discarded. Then I took a closer look at the window.open() API, link address http://www.w3school.com.cn/jsref/met_win_open.asp. window.open() defaults to the get submission method. If you want to implement the post submission method, you have to think of another way. Refer to http://www.jb51.net/article/32826.htm, here is a method. It is also a commonly used method. I modified it slightly according to the actual situation:

Copy code The 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.target=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(){});                                                                                                 }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 can be modified according to actual needs. data1 and data2 are the parameters that need to be passed to the action. In addition, Javascript event browser compatibility issues need to be considered here. I commented the function openWindow() here, otherwise an extra blank page (about:blank) will be opened. This basically meets the needs.

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!