Google
 

Saturday, July 14, 2007

Posting dynamic data to a popup page

It's a common need to pass some JavaScript dynamically formatted data to a pop up page, this can be easy as you just need to append data to the URL of the pop up and open it using window.open() or simply by setting the src property of an anchor element, this is a get request.

But if the data is too big to be put in the query string (2kb+), or if you want to make it harder for the user to see and modify the passed parameters, you'll need to post this data.

this JavaScript function makes the job, I post the string ("real long data").

function PostToPopup()
{
window.open("about:blank","popup",null,null);

var hdn= document.createElement("Input");
hdn.type="hidden";
hdn.name="popupdata";

var frm= document.createElement("Form");

frm.target="popup";
frm.method="POST";
frm.action="popup.aspx";

document.body.appendChild(frm);
frm.appendChild(hdn);

hdn.value="real long data";

frm.submit();
}


You can read this value in the popup from the request..this is an ASP.NET example:
Response.Write(Request.Form["popupdata"]);

No comments: