5

I know this has probably been asked before, but here goes: I have a web application that needs to generate modal dialogs. alert, confirm, and prompt are too simple and ugly, and that modal window function...it's a long story. I can't use it. So, I'm going to create the modal box using DOM functions and CSS. However, I need to put quite a lot of content into the dialog, and I'm wondering what the best way to do this is. Putting the HTML into a string and using innerHTML is unwieldy. I could use the DOM, but that's annoying and takes too much time to code. I know I can use a script with a weird type tag (something like x-random/x-htmlstuff) and then copy it's content to the innerHTML, but is there a better, more "official" way to do this?

3
  • Are you generating the HTML dynamically? If not, you can just put it into your original HTML, with display: none. Then change it to display: block when you want to show the dialog. Commented Dec 24, 2013 at 23:14
  • is that how I should do this, then? Commented Dec 24, 2013 at 23:15
  • Better to use '' (empty string) rather than 'block' so that displayed elements adopt their default or inherited style (which might be 'block', but might also be 'inline-block', etc.). Commented Dec 25, 2013 at 2:46

6 Answers 6

4

if the layout of the modals are static, just put them into the HTML of the page. Use CSS to set them to display: none when the page is displayed normally. When you want to display the model, use

document.getElementById('modal-id').style.display = 'block';
Sign up to request clarification or add additional context in comments.

5 Comments

An improvement on this would be to use jQuery<3: $('#modal-id').show() //.hide() to hide the modal
The question didn't say jQuery, so I didn't assume it in the answer.
It's just a suggestion ;)
I personally prefer jQuery, but if the question doesn't ask for jQuery I only suggest it when the raw JS solution would be overly complex. Other answers already suggested jQuery and the OP said they weren't acceptable.
Better to use ...style.display = ''; so elements adopt their default or cascade style.
1

I've heard that some people use this solution:

<script type="text/html" id="popup_html">
 html...
</script>

(of course, you should make it invisible)

But, most likely, if you're trying to write a lot of HTML from javascript, then you should retrace and think if there's a better way.

  1. If you're using the same div multiple times, you should just create it in the HTML page, and display it when needed
  2. if you're creating a new element - see if you can use the document.createElement and appendChild methods (assuming there aren't many nodes involved)
  3. if neither apply - retrace. For large projects, maybe object-oriented javascript can help.

1 Comment

I'll upvote it, but I already explained I know about this in the question, so there's nothing new to me here
0

There's no magical way that I'm aware of. I usually just use innerHTML and write the HTML out in a well formatting from such as:

box.innerHTML = "<div id='boxChild'>\n" +
                "    <p>Put whatever content here</p>\n" +
                "</div>";

The \n make it so if you view your code, it will be well formatted, and no one long string once the JS writes it.

2 Comments

interesting. Not the easiest thing ever, so I'm not going to accept it. I said in the question I didn't want to just do this
Fair enough. Sometimes you don't have a choice if your content is dynamic, or it's the easiest way. But it seems like you're not dynamically generating the messages, so the solution you chose is definitely the best.
0

A way to do this, is to generate the popups within the html and show or hide them when you need, like this:

<div class="myPopup">
    <div class="pop-message msg-01">This a pre generated alert with the id: <span class="dynamic-field-01"></span></div>
    <div class="pop-message msg-02">This another pre generated alert with the id: <span class="dynamic-field-02"></span></div>
    <div class="pop-message msg-03">...</div>
</div>

.pop-message {
    display: none;
}

Now while user navigates the page, you are going to hide and show the .pop-message's while replacing those .dynamic-field's if needed.

Comments

0

I would suggest having the HTML for your modal content in separate files, and then loading it asynchronously when you need it to popup the modal.

partials/modal.html

<div class="content">My modal content</div>

main.js

var modalContent = null;

function _fillModal() {
    modal.innerHTML = modalContent;  // something like this
}

function openModal() {
    if (!modalContent) {
        // XMLHttpRequest, which populates the modalContent variable
        // and in the callback, calls _fillModal()
    }

    // If already filled, just call
    _fillModal()
}

If you want the content to be dynamic, make modal.html a template, and use a JS template library (for example http://underscorejs.org/#template), or write a simple RegExp replace yourself.

2 Comments

interesting idea, but I don't want the user to have to wait for a request to complete, delaying the modal
In that case, you can perform the AJAX request when the page loads, and by the time the modal needs to open, the content (or template) will be available.
0

I'd suggest loading it with innerHTML or using jQuery to simplify things, but if you need a modal window, could you use the jQuery UI modal dialog, shown here?

If you have the content loaded in divs in your HTML, and have them have css display:none;, and then show them with

document.getElementById("unshown-div").style.display="block";

If you can use jQuery, a modal box could be done with

<div id="modal" style="display:hidden">
    Here is a modal dialog bbox
</div>

and your script:

$("#dialog").dialog();

Whatever you do, just don't use document.write()

2 Comments

sorry, I'm not using jQuery
@Markasoftware ok I added a couple of non-jQuery solutions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.