window.open of implements post to pass parameters

  • 2020-05-12 02:13:06
  • OfStack

In actual project, often meet such requirements, namely the implementation subsystem to jump between pages and open the new page, my team is using SSH framework, so url are similar to the * * * *. action, also name ID and system (system) with two parameters, two parameters are struts intercept in the session, in opens the page of the subsystem and a ztree plug-in implementation of tree menu ID parameter system is needed to initialize, Using window.open (url,"_blank") directly will make url too long and expose 1 parameter at the same time. Therefore, I want to use post to submit and hide the delivery of parameters in the submission process. First of all, I want to submit ajax, but there will be problems in the delivery of the two parameters. ajax submission and window. open() will make action go twice, so I will leave it out. Again carefully watched window. open API (), link address http: / / www w3school. com. cn/jsref/met_win_open asp. window.open () defaults to get submission, so you'll have to think of something else if you want to implement post submission. Reference / / www. ofstack. com article / 32826. htm, here introduces a kind of method. It's a very common method. I slightly modified it according to the actual situation:


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(){});        //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 as needed. data1 and data2 are the parameters to be passed by action. There is also the issue of Javascript event browser compatibility to consider here. I have commented function openWindow(), otherwise it will open one more blank page (about:blank). So that basically meets the demand.

That's all for this article, I hope you enjoy it.


Related articles: