Click here to Skip to main content
15,126,498 members
Articles / Programming Languages / Javascript
Tip/Trick
Posted 30 Apr 2012

Stats

23.1K views
5 bookmarked

Jquery Alert Control

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
30 Apr 2012CPOL1 min read
This is a very simple and easy to implement alert control using jQuery

Introduction

We often need to interact with the user at client side for different validation purposes or any other guideline purposes. For all these we prefer easy, light and simple techniques. Alerting the user at client side must be very pretty because users on the Web are now in a habit of attractiveness in everything that they face on the web. I have developed a Message Box using jquery that is very easy to use and looks very pretty. It provides a simple way for developers to raise unobtrusive messages to the end-user.

Background

I named it braviMessageBox. This pop up message box is a floating window that contains a content area. The dialog window closed with the 'x' icon by default.

If the content length exceeds the maximum height, a scrollbar will automatically appear.

Using the Code

braviMessageBox is very easy to use: You need to just add a reference to Jquery and add the braviMessageBox.js file with the braviMessageBox.css.

A quick code look "How to use" is here.

XML
<input type="button" id="btnTest" value="Click Me!" 
onclick="ShowMessage('Testing bravi Message Box..<br />
this is a very pretty jquery message box control!');" />

The technique used in the js file is just create a DIV element and append it to page BODY.

JavaScript
function ShowMessage(message) {
 $("<div class='msg' id='dvMsg'><table width='100%'><tr><td width='90%'>" + 
message + "</td><td width='10%' valign='top' style='float:right;text-decoration:none;'>
<a href='javascript:void();' onclick='closethis(myMessage);'>x</a>
</td></tr></table></div>").attr('id', 'myMessage').appendTo('body');
 setTimeout("$('#myMessage').fadeOut('slow');", 2900);
 setTimeout("$('#myMessage').remove();", 4000);
}

Though this message box will disappear after some time, you can still close it. There is the close method.

JavaScript
function closethis(id) {
 $('#myMessage').fadeOut('slow');
 setTimeout("$('#myMessage').remove();", 1000);
}

Now there is just one CSS class to position the Div in the center of the screen.

The CSS class is:

CSS
.msg
 {
  background-color: #4f7eaa;
  text-align: left;
  position: absolute;
  top: 35%;
  left: 40%;
  margin-left: -70px;
  width: 350px;
  height: auto;
  padding: 10px 10px 8px 10px;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  -khtml-border-radius: 10px;
  border-radius: 10px;
 }

Image 1

Future Programs

This was my first Post on a Forum. Now I aim to continue it and want to enhance this to a similar pop up dialog page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

tanweer
Software Developer LIFELONG Pakistan, Islamabad
Pakistan Pakistan
Software Engineer at LIFELONG Pakistan,

http://tanweerbravi.blogspot.com

Comments and Discussions

 
BugCS0234: The type or namespace name 'Tasks' does not exist in the namespace 'System.Threading' (are you missing an assembly reference?) Pin
Mukund Thakker16-May-12 21:10
professionalMukund Thakker16-May-12 21:10 
GeneralRe: CS0234: The type or namespace name 'Tasks' does not exist in the namespace 'System.Threading' (are you missing an assembly reference?) Pin
tanweer16-May-12 23:35
Membertanweer16-May-12 23:35 
QuestionI Agree Pin
Anurag Sarkar4-May-12 7:15
MemberAnurag Sarkar4-May-12 7:15 
QuestionAdd a screenshot Pin
Jason Vogel30-Apr-12 12:54
MemberJason Vogel30-Apr-12 12:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.