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

 

About Author

Author Image
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

Request for Proposal

Name is required

Comment is required

Sending message..