msgBox 1.0 a Custom Javascript Messaging Framework
msgBox v1.0 JS
It's funny how I come about my projects. This one started after someone discovered the FireFox bug in my FormFieldinator framework. The error has to do with the a form field losing focus after you call an javascript alert or confirm window. I started to wonder if I could create a custom messaging system that would avoid this problem. And then at the same time, I had a client that wanted to show a window on their website asking their users to fill out a survey.
All of this led me to create a javascript messaging framework called msgBox. Now, it hasn't been fully tested and I'm sure some of you are going to point out improvements or <gasp> problems. But I think it's a good start. So without further adieu, let me introduce to you msgBox v1.0
Test 1 - msgBox.init
If you are looking at this blog entry, then you've already experienced the msgBox init on the page load. It's the annoying message that pops up everytime you load the page. You define this action like so:
msgBox.init({
message : "<p>I would like to introduce msgBox v1.0<\/p>",
});
msgBox.show
You can also call msgBox within your scripts like you would any other system message by passing the text as an object or a simple string.
Test 2 - msgBox.show()
The following two examples have the same result, but the first one shows how you can just supply a string to bring up the default message window.
msgBox.show('<p>You clicked me!</p>');
msgBox.show({
message:'<p>You clicked me!</p>'
});
Test 3 - msgBox.show()
You have two options for the animation types. Dissolve and slideDown. slideDown will show the background dissolve in and the have the message window slide in the from the top of the screen.
msgBox.show({
message:'<p>I\'m like the first two test, but different.>br /<I slide in style!</p>',
type:'error',
animationType:'slideDown',
duration:500
});
Test 4 - msgBox.show()
Another feature is that you can set the action and cancel button text and define what action to take once they are clicked. This example also uses a custom icon and message type.
msgBox.show({
title : 'Creative Juices Bo. Co.',
type : 'Message of the Day',
message : '<p>Would you like to visit the best place on the web?</p>',
icon : 'http://www.cjboco.com/includes/msgBox_v1/_etc/images/msgBox_icons/Crystal_Clear_app_clock.png',
backdropOpacity : 0.3,
duration : 0,
butOkMsg : 'Yes',
butOkAction : 'http://www.cjboco.com/',
butCancelMsg : 'No thanks'
});
Defined Settings
msgBox was designed to be versitile messaging system that can be used to replace simple alerts or complex confirmation messages. The core look and feel can easily be changed through the use of a stylesheet, but you also have the ability to pass various attributes to change how it functions. Below are a list of all user defined attributes that can be modified to your liking.
Standard Attributes
| Attribute | Description | Values |
|---|---|---|
| title | The text that is displayed in the header bar. | String |
| message | This is your message text. This can be styled using HTML syntax or be plain ASCII. | String |
| type | The title that is displayed above your message text. Used in conjunction with icon. There are two built in settings, "alert" and "error", which will display a default icon. If left blank, will default to "alert". | String or "alert"|"error" |
| icon | This is a path that leads to your custom icon. Path must be an absolute path or relevant to the HTML page that is displaying the msgBox. If icon is left blank with a custom type, no icon is displayed. | URL |
| animationType | The type of animated effect to use to reveal the msgBox. Options are "dissolve" and "slideDown". Defaults to "dissolve" | "dissolve"|"slideDown" |
| duration | The speed in which the animation is played. Time in milliseconds. Default 350. | Numeric (0 - n) |
| backdropOpacity | The final opacity of the background plane once the msgBox loads. The backdrop will always dissolve in at the same speed as set in duration. This value should be from 0 to 1. Default 0.75. | Numeric (0 - 1) |
Icon Attributes
| Attribute | Description | Values |
|---|---|---|
| iconPath | The default absolute or relative path to find the "icons" directory. If left blank, will default to "_etc/images/msgBox_icons/". | URL |
| iconAlert | The default alert icon file name. If left blank, will default to "Crystal_Clear_app_error.png". | URL |
| iconError | The default error icon file name. If left blank, will default to "Crystal_Clear_action_stop.png". | URL |
Button Attributes
| Attribute | Description | Values |
|---|---|---|
| butOkAction | The only option at this time is to provide a link to follow. | URL |
| butOkActionTarget | If a link is provided you can specify to open in the current window or in a new one. If left blank, defaults to "_blank". | "_self"|"_parent"|"_blank" |
| butOkMsg | The text displayed in the button. If left blank, defaults to "Ok". | String |
| butCancelAction | The only option at this time is to provide a link to follow. If left blank, defaults to close the msgBox window. | URL |
| butCancelActionTarget | If a link is provided you can specify to open in the current window or in a new one. If left blank, defaults to "_self". | "_self"|"_parent"|"_blank" |
| butCancelMsg | The text displayed in the button. If left blank, defaults to "Cancel". | String |
So now what?
So now, I have the first version complete, I need to perform some more testing and make sure all the browsers like it. I eventually want to add some cookie setting abilities to the msgBox init function, so you can display it once per day, week, etc. In the mean time, go ahead and grab a copy of the source code below and start playing around with it. And as usual, if you see any problems let me know.
nice script. I've got 2 ideas that come to my mind.
1.: After showing a message box like an alert box you can still click the elements on the normal page. Maybe some option like "Modality" would be nice so that the user first have to click a button before he can do something else.
2.: It would be easier if the focus would be set to one of the available buttons. As it is now (I only tested it in Firefox) the focus is still on the element you last focused on the normal page and not on the buttons of the MsgBox.
Thanks for the tips. I especially like #2. I didn't think about adding a default button. More importantly I think I need to add keyboard input.
#1 is possible. I think I'll add an option for that.
Your script doesn't work on IE browser. My security is set to medium. Do I need to enable something? When I hit your test buttons, I get the message "Error on page" in the lower left corner.
Hey Sean, Nope, I just tested it in IE and there seems to be an error in there. Let me take a look at it and see what's going on.