A bit of technology

news, thoughts, articles and a bit of fun!

What to do when returnValue of modal window always returns undefined in Internet Explorer?

, , , , , , , ,

Today I was fighting with IE8 for a few hours because this stupid browser enjoys torturing web-developers. There is a method in JavaScript in IE called window.showModalDialog which opens modal popup. This method returns value which was set by page opened in such popup. This is done by setting window.returnValue inside popup, but guess what? It does not work! There are a lot of reasons for this feature not to work and a lot of local workarounds, but that all suck, so I killed a lot of time to find An Ultimate Solution to situations when returnValue is undefined. And it is quite simple (: As always!

On caller page create a callback function which will be called from popup to update data:

function modalCallback(newData) {
    globalVar = newData;
    // globalVar is some external variable you would like to update.
    // Actually you may place any code here!
}

Then prepare dialogArguments on a caller page and execute showModalDialog:

var args = {'callback': modalCallback};
window.showModalDialog('popup.html', args);
// Put the code to process recieved data here!

And now for a modal popup. When you ready to send data back to parent window just call your callback function!

window.dialogArguments.callback(dataFromPopup);

That's it!

P.S. I hope people using this tip do know JavaScript and programming in general.

3D Realms Shuts DownLinks: Why Linux is not (yet) Ready for the Desktop

Comments

Unregistered user Friday, July 10, 2009 12:19:58 PM

Anonymous writes: nice!

Unregistered user Sunday, September 13, 2009 11:59:15 AM

Anonymous writes: Thanks a Lot. i was searching for this from last few days. it's realy simple and gr8 solution.

Unregistered user Thursday, October 15, 2009 9:07:42 AM

Anonymous writes: yeah, great , thank you very much :))

Unregistered user Friday, November 20, 2009 11:01:03 AM

Anonymous writes: What if I am not allowded to change the content of the modal dialog?

Aux Saturday, November 21, 2009 12:01:53 PM

Then you're screwed.

Unregistered user Thursday, September 2, 2010 1:34:09 PM

Sandeep Shukla writes: That is a life saver: Thanks a million !!!!

Unregistered user Wednesday, August 3, 2011 5:50:10 AM

Anonyme writes: Oh god I hate javascript. But thanks, really helpful. With that I don't need to suicide anymore :D

Unregistered user Wednesday, August 10, 2011 9:25:17 AM

Sagar writes: Thanks. It helped.

Unregistered user Friday, January 27, 2012 1:49:23 PM

curt_russel writes: В Хроме эта байда не работает! Говорит, что не знает свойства callback. Че делать в этом случае? А в Сафари работает!!

Unregistered user Monday, January 30, 2012 7:04:30 AM

curt_russel writes: I was mistaken - window.dialogArguments is undefined in Chrome! Is there any possibilities to improve this solution for working in Chrome? In our huge project this is the only solution, i'm afraid, to cause working on every browser.

Aux Tuesday, February 7, 2012 9:58:43 AM

modalDialogs are IE only feature. Use popups.

Write a comment

New comments have been disabled for this post.