How can I make a program wait for a modal window to close?

Essentially, I’m trying to recreate the “alert” and “prompt” functions, but in a single function that uses a modal window. I have it set up so that when one of the function’s parameters is set to 1, it adds a text field to the modal window and records that text field’s value when it closes. I’m having trouble making it so that when the window is opened, the program waits until the window is closed again. How can I do this?