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!
Enjoy this article?
Pages
Friends
Recent Posts
- Automatic Get/Set Visual C# 2010
- C#/PHP Database Object Generator/Database Access Layer Generator
- Beginner Game programming jobs in Toronto
- HostGator 2010 Black Friday Sale Special 50-80% off hosting!
- Paging in PHP – PHP Paging Tutorial
Buy me a Beer!
Categories
- ASP.NET
- C#
- Cheap Hosting
- Databases
- Finance/Investing
- General
- Javascript
- MySQL
- PC Maintenance
- PHP
- Programming
- SEO/Marketing
- SQL
- Uncategorized
- XHTML/CSS
November 2nd, 2009 - 18:53
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.
November 2nd, 2009 - 21:20
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.
January 20th, 2010 - 08:47
This is good article for beginners, all the functions given are working and anybody can easily implement them into projects. Thanks.
September 3rd, 2010 - 05:05
Awesome!!! Thanks a ton!!!
I have been looking for such a functionality for fews days now.
September 28th, 2010 - 08:27
nice……Thankyou……
December 8th, 2010 - 07:40
jquery alerts plugin is not downloading.. i need the script.. i am exactly looking out for this thing…
December 8th, 2010 - 09:55
Hi Asif,
I will email you the plugin – as well as make it available in that document.
Please feel free to donate if you like the blog!
January 17th, 2011 - 23:00
I have problem with IE6, the confirmation pop up is displayed at the bottom of the screen. How can I fixed this issue? Thanks
March 18th, 2011 - 12:41
As a web developer – I no longer support IE6.
IE6 has many security flaws and is just not standards compliant.
Sorry!
March 16th, 2011 - 23:50
Hi
I’m unable to get jAlert and jConfirm next to next .Please help me implement.
March 23rd, 2011 - 22:51
where is the css download
April 25th, 2011 - 05:21
How will i download the css file?
June 20th, 2011 - 01:01
the css has been added. ive been really busy – but there you go
May 19th, 2011 - 03:58
what browser do you use?
i have the same problem as knguyen, but i am using IE 8.0.
August 4th, 2011 - 10:28
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.
August 17th, 2011 - 11:24
This is supported – check my example above. You need to write your code as the callback function to the jconfirm.
August 17th, 2011 - 11:25
jConfirm(‘Would you like Ice Cream’, ‘msgBox Title’, function(r){
if(r){
alert(‘you replied yes!’);
}else{
alert(‘you replied no!’);
}
}