The Infinite Loop – Beginner's SEO, Beginner C# & JQuery Tutorials Problem. Problem Solved. Loop. – The life of a programmer

21Jul/0917

Custom Javascript Alert and Confirm Dialog boxes

This article will teach you how to make Custom Javascript Alert and Confirm Dialog boxes, and change the button text on dialog boxes.

For the most part, custom alert and confirm dialogs are not possible because alert and confirm dialog boxes are handled by the browser, however, there is a great JQuery plugin that will let you customize your alert boxes. If you don't know JQuery, do not worry, because i'll take you through it step by step. This little customization can make your web application that much more professional.

This will show you a little tutorial on how to make the custom alert and confirm dialogs AND How to return values from a custom confirm dialog box. This is EXTREMELY important. I have seen a lot of tutorials on how to do this, but none of them talk about actually retrieving the value of the response in a custom confirm dialog box..

Custom Javascript Alert Dialog Boxes

Step 1 : Download JQuery

Click on Current Release, you can click Uncompressed if you want to inspect the code, but you should use minified version for production.

In our site, we will make a folder called scripts, and we will put the jquery.js file into this folder.
We will also make a new file in the same folder named jquery.scripts.js.

Step 2: Download jquery.alerts (Right click, Save as) and the css here: jquery.alerts.css
NOTE: This plugin is no longer supported by http://abeautifulsite.net/, so use it at your own risk.

We will put the jquery.alerts.js into the scripts folder, and the jquery.alerts.css into a styles folder.

Step 3: The Setup
In our webpage we will add the references to these file.

<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.scripts.js"></script>
<script type="text/javascript" src="scripts/jquery.alerts.js"></script>
<link href="styles/jquery.alerts.css" rel="stylesheet"/>

Once we have all these references in, we're ready to write some jquery.

Step 4: Open jquery.scripts.js
Type

$("document").ready(function(){

});

This sets up a function to be run when our document is ready.

In this document.ready function, type
alert('jquery loaded');

Refreshing your webpage, you should see an alert saying "jquery loaded", if you see this, you have setup jquery correctly. If you do not, check the steps above, or hit CTRL+F5, this does a cache refresh and requests a new copy of all the javascript and css files.

We will add a button to our site.
<input type="button" value="Custom Alert" id="customAlert"/>

In our document ready function, we will hook up a handler to handle the "click" event of this button.

$("document").ready(function(){
$("#customAlert").click(function(){
alert('this is an alert');
});
});

If you refresh your webpage (CTRL+F5) and click the button, you should see a normal javascript popup.
Now, change the alert to jAlert.
jAlert('this is a custom alert', 'this is a custom title');

Upon refreshing the page, and clicking the button, you should see a custom grey alert box. The styles can be modified by editing jquery.alerts.css.

If you'd like to read on Custom confirm dialog boxes, please continue reading.
By this point, jquery and the jquery alerts plugin and css should be setup.
Follow the above tutorial until you get to the actual jAlert method.

Comment if this helped! :)

Custom Javascript Confirm Dialog Boxes

Now, we will do a Custom Javascript Confirm Dialog.
The confirm function looks like this
jConfirm('msg', '[title]', [callback function]);

$("document").ready(function(){
$("#btnID").click(function(){
jConfirm('Would you like Ice Cream', 'msgBox Title', function(r){
if(r){
alert('you replied yes!');
}else{
alert('you replied no!');
}
}
});
});
This is the basic implementation of custom confirm boxes, however you can still customize these a little further.

Changing Javascript Alert Button Text

Before calling jAlert or jConfirm, you can set the button text.

$.alerts.okButton="OKAYYYYYYYYYY";
$.alerts.cancelButton="CANCELLLLLLLLLL";

This is also useful for localization.
For example in an english jquery.scripts.js you can have.

$.alerts.okButton = "yes";
$.alerts.cancelButton = "no";

And in a french jquery.scripts.js you can have

$.alerts.okButton="oui";
$.alerts.okButton="non";

Hopefully this helped you add that little extra touch.
For reference, here are the 2 methods, only the msg parameter is required.

jAlert('msg', '[title]', [callback function]);
jConfirm('msg', '[title]', [callback functoin]);

There you have it!
Please comment if this was helpful :)
Goodluck with your custom javascript!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Comments (17) Trackbacks (0)
  1. Thanks for the post. I just used this technique on site the needed modification of recurring calendar events. When an event that repeats is being modified, I use this custom popup with the text “change this event only” or “change all occurrences”. The default “Ok” and “Cancel” wouldn’t cut it. I was worried I might have the html for a whole new popup window, but this is much cleaner.

  2. no prob. glad it helped. Ok / Cancel could be applied to your situation, but it doesnt make sense from a usability point of view. We have to reword things to fit the Ok/Cancel. Glad this post helped you at.

  3. This is good article for beginners, all the functions given are working and anybody can easily implement them into projects. Thanks.

  4. Awesome!!! Thanks a ton!!!

    I have been looking for such a functionality for fews days now.

  5. nice……Thankyou……

  6. jquery alerts plugin is not downloading.. i need the script.. i am exactly looking out for this thing…

  7. I have problem with IE6, the confirmation pop up is displayed at the bottom of the screen. How can I fixed this issue? Thanks

  8. Hi

    I’m unable to get jAlert and jConfirm next to next .Please help me implement.

  9. where is the css download

  10. How will i download the css file?

  11. what browser do you use?
    i have the same problem as knguyen, but i am using IE 8.0.

  12. I like the library, its easy to integrated into existing code, but what its missing (like all other custom dialogs out there) is, it does not stop the execution of the script when the dialog pops open like in the case of the built in alter() and confirm() dialog boxes. It’s unfortunate that this cannot be achieved through scripting.
    What I need is to stop/pause the execution until the user clicks “Ok” on the alert box or “Yes/No” on the confirm dialog box, and based on the response take the next action. But this is not happening right now. :-(


Leave a comment

No trackbacks yet.