Posted on Monday, May 19th 2008 at 20:32

Modalpreview - Preview Your Comment in a Modal Window

7 comments so far | Digg | del.icio.us

Some people have asked me about the comment preview functionality that DevKick is using in the blog, so I made it into a handy jQuery plugin called Modalpreview.

Modalpreview image

I got the idea when was looking for a quick way of adding a preview function to the comment form here on DevKick. I wanted something new and did not want to go server-side. I never liked the live preview thing, mainly because I don’t like when stuff happens on the page automatically when I press a key. So I thought, why not just use a modal window to present the textarea as HTML before submitting the form?

That would prove to be a lot harder than it sounds like.

The first problem: fixed positioning in IE

Modal windows would be really easy to implement if it wasn’t for the fact that IE6 is lacking support for position: fixed. Fortunately, I found some expressions that works well enough for producing modal windows, but the vertical centering is still buggy. That’s ok though, we can always specify a top margin for IE from the modal window up to the browser edge. Anyway, here is the key for adding position:fixed to IE:

top:expression(
    ((ignoreMe = document.documentElement.scrollTop ?
    document.documentElement.scrollTop :
    document.body.scrollTop)) + 'px'
);
right:expression(
    ((ignoreMe2 = document.documentElement.scrollLeft ?
    document.documentElement.scrollLeft :
    document.body.scrollLeft)) + 'px'
);

Second problem: IE’s stacking order

I wanted to append the preview div inside the form element to integrate form functionality, but IE said no. IE has it’s own z-index order that I never understood and I believe it’s some crazy relative logic depending on the nearest positioned ancestor etc. So trying to break out the preview div from the form falied miseably in IE, since it was impossible to place it above the overlay and position it fixed in the body center. This is the main reason why I couldn’t have a submit form button inside the preview division, wich leads us to the next problem:

Third problem: the submit() function

I really, really wanted to clone the form’s submit button and append it to the preview division to add a ’submit form’ button under the previewed text. But in order to do that properly, the preview division needed to stay inside the form tag. That proved to be impossible in IE6 as described earlier, so the other option was to add a simple submit() function to a button.

That could have worked, but since many form elements already use the name="submit" attribute, it will effectively disable the integrated submit() function in javascript. Not even a $('form *[name=submit]').attr('name','_submit') would work, since IE maps the form before DOM ready. So no go there.

The result

In the end, I came up with a cross-browser alternative that works pretty good, even if the modal window can’t submit the form. The overlay and preview division is appended to the body in order to always stay on top and centered. I resorted to a simple close button, which is fine since you might want to have a last look to see if you filled in all the other required form fields. You can see it in action just here below.

When dealing with stuff like z-index, fixed positioning and modal windows, IE is a real pain. If any of you can find a cross-browser solution for submitting the form inside the modal window, you would be my hero.

Links:

Leave a Reply




Note: rel="nofollow" applied. Spammers step back.
No HTML allowed except <p> (paragraph)

7 Responses so far.

Permanent link At 8:14 am on October 6th, 2008 , "> wrote:

“>

Permanent link At 8:15 am on October 6th, 2008 , >" wrote:

>”

Permanent link At 8:16 am on October 6th, 2008 , ">alert("Hacked") wrote:

“>alert(”Hacked”)

Permanent link At 9:14 pm on October 21st, 2008 , sadf wrote:

sadfasdf fsdfsdf sdf

Permanent link At 3:40 pm on October 24th, 2008 , tester12345 wrote:

Very interesting…

Permanent link At 11:55 am on November 4th, 2008 , XpycT wrote:

Cool =)

Permanent link At 3:40 pm on November 18th, 2008 , johnners wrote:

Yep - that works

DevKick News RSS

24 Kick Ass Portfolio Designs
exactly.
 17th of June at 9:07 pm
Using CSS to Fix Anything
Noupe shares some quick tips on how to avoid easy pitfalls when creating your CSS layout.
 17th of June at 11:14 am
2008 Design Trends
So what's hot now' Pencil sketches, handwritten notes, card stocks, watercolor effects, collage art, script fonts, grungy and splatter ink backgrounds etc... Some nice visual examples.
 16th of June at 10:04 am
10 Video Tutorials for Learning Basic Web Design Skills
Some people can read instructions on how to do things and can immediately go out and do them without any problems. But others need to see things done before they fully grasp how to do them.
 16th of June at 10:03 am
UTF-8: The Secret of Character Encoding
Character encoding and character sets are not that difficult to understand, but so many people blithely stumble through the worlds of programming without knowing what to actually do about it.
 12th of June at 11:17 am
jQuery UI v1.5 Released
"When we first started with the UI project, we set out to build a generic, basic, and simple way of adding and extending core interaction to DOM elements. However, we soon found that our approach wasn't working for UI."
 10th of June at 9:55 pm
The PHP Benchmark
The PHP Benchmark was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed. You may be surprised at the results that this page generates.
 9th of June at 1:27 pm
Introduction to CSS3 - What is it?
This article marks the first of several, providing an introduction to the new CSS3 standard which is set to take over from CSS2.
 9th of June at 9:24 am
Planning a Semantic Web site
This article leads you through the aspects of both information architecture and general infrastructure you need in place to truly take advantage of this burgeoning opportunity.
 8th of June at 5:49 pm
Why we skip Photoshop
7 reasons why the 37signals team skips photoshop.
 8th of June at 8:25 am

From the lab

Latest Components RSS

Component Categories