15

I have seen many websites that have personalized their alert and confirmation boxes with different button text.

By default, this is what the JavaScript window.confirm() function would output:

+-----------------------------+
|Message from webpage         |
+-----------------------------+
|Popup text                   |
|                             |
|                             |
|                             |
|                             |
|   [Ok]     [Cancel]         |
+-----------------------------+

But, what if I wanted some customization like custom header text, and custom button text?

+-----------------------------+
|My custom Header             |
+-----------------------------+
|Popup text                   |
|                             |
|                             |
|                             |
|                             |
|   [HELLO]     [GOODBYE]     |
+-----------------------------+

Can this be achieved in JavaScript, Jquery, or AJAX (or another language)? If so, could you please show me an example? Thanks!

rob waminal
  • 18,117
  • 17
  • 50
  • 64
pattyd
  • 5,927
  • 11
  • 38
  • 57
  • 3
    Javascript modals are [notoriously difficult (or impossible) to alter](http://stackoverflow.com/questions/823790/javascript-confirm-popup-yes-no-button-instead-of-ok-and-cancel). Your best bet is to make your own modal popup. Consider jQuery UI: http://jqueryui.com/dialog/ – showdev Jun 05 '13 at 00:04
  • 3
    FWIW jQuery and AJAX aren't languages – Madbreaks Jun 05 '13 at 00:05
  • 1
    This is *possible*. There are a couples of post on this site where you can find useful information. e.g.: http://stackoverflow.com/q/6929416/1505348 – Lucio Jun 05 '13 at 00:08
  • @Madbreaks what is FWIW? Anyways, you knew what I meant ;) – pattyd Jun 05 '13 at 01:35
  • FWIW == For What It's Worth – KingCronus Jun 05 '13 at 08:50
  • @pattyd No, I don't know you, your statement led me to believe that you think jQuery and AJAX are programming languages. – Madbreaks Jun 05 '13 at 19:42

3 Answers3

7

The simple answer is that you can't , prompts do not support this feature,

However you could use Jquery or build your own modal/ dialog to emulate this functionality.

See the jQuery UI Dialog for ideas and examples.

Setting up the example from the page check out the jsfiddle

HTML:

<div id="dialog-confirm" title="Empty the recycle bin?">
  <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

<p>Sed vel diam id libero <a href="http://example.com">rutrum convallis</a>. Donec aliquet leo vel magna. Phasellus rhoncus faucibus ante. Etiam bibendum, enim faucibus aliquet rhoncus, arcu felis ultricies neque, sit amet auctor elit eros a lectus.</p>

JS

$(function() {
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height:140,
      modal: true,
      buttons: {
        "Delete all items": function() {
          $( this ).dialog( "close" );
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      }
    });
  });

The CSS is to large to post, but you can link this file :

http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css

isJustMe
  • 5,452
  • 2
  • 31
  • 47
7

I wont discover anything new here. And the comments created say all the most relevant information.

You should look at open source alternatives that have great support and are easy to customize. Take a look at BootBoxJS (it depends on jQuery).

In that site you can find examples of alerts, dialogs, and other useful tools. e.g.

confirm dialog personalized

To use the mentioned soft:

  1. Download the js library compressed (including js, css, etc)
  2. Unzip the file whatever you want to.
  3. Then copy and paste the example code and save this as example.html or whatever you want to.
  4. Now change the links to make it point to your downloaded files (see how I changed my file)
  5. Save the file and test it with your preferred browser.

I just did it and it works perfect! (Under Firefox and Chromium)

NOTE: I tested it with the new version of BootStrap (v.3) and it doesn't work.

Lucio
  • 4,753
  • 3
  • 48
  • 77
  • Hey @Lucio, I actually like this allot. I cant get it to work though! Could you give some installation steps? – pattyd Jun 05 '13 at 17:01
  • See my updated answer. I tested it by my self. Hopefully, will be useful :) – Lucio Jun 05 '13 at 21:43
  • :) also, I cant find where to download the js library compressed... that link just brings me to the home page – pattyd Jun 05 '13 at 22:49
  • 1
    Oh men, you're vague. [Here is the link](https://github.com/makeusabrew/bootbox/zipball/v3.2.0). You need to read more :) – Lucio Jun 05 '13 at 22:51
  • what am i doing wrong? I seriously cant get this working :( http://pastebin.com/ijyhvhCR – pattyd Jun 05 '13 at 23:14
  • 1
    @pattyd I've created [this chat room](http://chat.stackoverflow.com/rooms/31288/room-for-lucio-and-pattyd) to explain further. – Lucio Jun 05 '13 at 23:24
1

No. A simple review of the docs may be useful. Java applets have similarly (but distinctly different) styled UI elements that may be what you've seen in the past.

If you wanted to you could create your own dialog boxes using HTML/CSS/JavaScript that act like user agent dialog boxes, but that's a lot of work for the result.

Madbreaks
  • 19,094
  • 7
  • 58
  • 72