Open popup window with http POST data Javascript
Posted By : Akash Sharma | 24-Feb-2014
Recently I had a task to open a popup window with post request data.The popup window was linked to an action in grails where I had applied filters.
If post request data matches the condition for action, only then I had open required view.
Below is the demo code for the functionality.
Let say currently I am on a page having url as :
www.myDomain.com/demo/
and the url for popup window to open is:
www.myDomain.com/demo/myPopupView.html
Then I only need to pass relative url from current page.
This is the link for opening popup:
<a href="javascript:openPopupPage('myPopupView.html','[email protected]', 25 )">myPopupLink</a>
Below is the javascript code for opening blank page with hidden fields and submitting the form to relative url.
function openPopupPage(relativeUrl, emailId, age) { var param = { 'emailId' : emailId, 'age': age }; OpenWindowWithPost(relativeUrl, "width=1000, height=600, left=100, top=100, resizable=yes, scrollbars=yes", "NewFile", param); } function OpenWindowWithPost(url, windowoption, name, params) { var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", url); form.setAttribute("target", name); for (var i in params) { if (params.hasOwnProperty(i)) { var input = document.createElement('input'); input.type = 'hidden'; input.name = i; input.value = params[i]; form.appendChild(input); } } document.body.appendChild(form); //note I am using a post.htm page since I did not want to make double request to the page //it might have some Page_Load call which might screw things up. window.open("post.htm", name, windowoption); form.submit(); document.body.removeChild(form); }
For more information follow this link
Thanks
Akash Sharma
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Sharma
Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis