Sharing Controller Context with a Visualforce Popup

"How do I popup a dialog in Visualforce".

I'm not sure what the deal is but this week I've received 3 emails asking me how to open a Visualforce popop window from a Visualforce page but both running in the same session. The question is asked fairly often on the message boards but there isn't a native solution.

The use case is that you have a Visualforce page with a commandbutton that launches a Visualforce popup. You want the popup to be able to access the controller's context so you can share state. Unfortunately this is not currently possible with Visualforce and Apex. Opening a new window will start a new request which initializes a new instance of the controller. You could pass some variables to the new controller via Javascript but making a callback to the opening Visualforce is tedious as best.

Depending on what your popup needs to do, the best solution might be to create a modal instead of a new Visualforce page. Dave Carroll has a great article titled, "Modal Dialogs in Visualforce using the Yahoo! User Interface Library", that shows how to use an In-Page DIV or Hidden DIV to create the modal. You can probably accomplish the same effect with other libraries but Dave's solution with YUI! is straight-forward, simple and well documented.