views:

38

answers:

4

Hi guys, how can I open a modal window using javascript only. I can't use own html-file so I have to pass html-data directly to method which will show window. Could you help me with this?


upd: I can't use any own library.

A: 

Plenty of simple javascript libraries out there to do this sort of thing. Thickbox is pretty cool.

Squeegy
+1  A: 

Try a jQuery "lightbox-style" plugin. My favourite is ColorBox which does what you require. Example:

$.fn.colorbox({html:'<p>Hello</p>'});
Dan Diplo
+1  A: 

Library-less solution:

Make a div filled with content, style it so that it floats over your content and has display:hidden on page load. With javascript set myDialog.style.display = 'block' and behold your modal.

This javascript may also add a background element that is styled to be translucent and to span the entire screen height and width, and blocks interaction to the page element under it.

In concept it's pretty simple really. It's more CSS than JS, and the logic is simply showing/hiding the modal dialog element.

And there is lots of edge case improvement you can do with animating the element in, sizing it to fit content better, etc. This is the sort of polish the libraries give you.

Squeegy