Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Jquery Alert Control

0.00/5 (No votes)
30 Apr 2012 1  
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.

<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.

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.

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:

 .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;
  }

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here