window.open submit content to a new window by post _javascript tips

Source: Internet
Author: User
The first way
A recent Web project that requires the ability to pass parameters across pages is one that needs to take the contents of the current page to the newly opened subform by passing an ID past and then reading the contents of the database in a new window. It's not much of a hassle, but if the content is stored in the database, it's just a draft state, and the user is often considered a bug. Consider a get way of passing, the need to serialize the content and then through the URL to pass, appears to be very bloated, and get the length of the delivery content is limited. Then think of the way to pass the post, the problem is that the open method can not set the request method, the general Web page post are implemented through form. If you only simulate the way the form is submitted, then the arguments in the open method that can set the properties of the form are not available. And finally find a way to do that. The combination of the two ways, the form of target set to the name of open and the same value, the browser automatically recognized the implementation of the content post to the new window.

It is interesting that the submit method directly by calling form cannot trigger the OnSubmit event, view the Help document, and manually trigger it, otherwise you will only see the page refresh without opening a new window. Only one parameter content is passed in the code, which can actually be passed multiple.
The specific code is as follows:
Copy Code code as follows:

<script>
function Openpostwindow (URL, data, name)
{
var tempform = document.createelement ("form");
Tempform.id= "TempForm1";
Tempform.method= "POST";
Tempform.action=url;
Tempform.target=name;
var hideinput = document.createelement ("input");
Hideinput.type= "hidden";
Hideinput.name= "Content"
hideinput.value= data;
Tempform.appendchild (Hideinput);
Tempform.attachevent ("onsubmit", function () {Openwindow (name);});
Document.body.appendChild (Tempform);
Tempform.fireevent ("onsubmit");
Tempform.submit ();
Document.body.removeChild (Tempform);
}
function Openwindow (name)
{
window.open (' About:blank ', name, ' height=400, width=400, Top=0, left=0, Toolbar=yes, Menubar=yes, Scrollbars=yes, Resizable=yes,location=yes, Status=yes ');
}
</script>

The second way
Copy Code code as follows:

function Openwindowwithpost (url,name,keys,values)
{
var newwindow = window.open (URL, name);
if (!newwindow)
return false;
var html = "";
HTML + + "if (keys && values)
{
HTML + + "<input type= ' hidden ' name= '" + Keys + "' value= '" + Values + "'/>";
}
HTML + + </form><script type= ' text/javascript ' >document.getelementbyid (' Formid '). Submit (); ";
html = "<\/script></body>NewWindow.document.write (HTML);
return newwindow;
}

The first method is recommended, the second is found in the test process, conflicts with the Calendar pop-up box, if the child page has a calendar pop-up box, the Point calendar box will not stop refreshing the page, does not pop the calendar box. Of course, it may also be the problem with the calendar box that I use.
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.