Alert! A JavaScript Popup!
Hi. I made a JavaScript popup. Have a look. You can use it, if you want.
It requires jQuery 1.4 or later.
Basic Useage
Alert( message [, options [, callback 1 [, callback 2 ]]] )
To use the Alert, simply create a new instance of the Alert class. The constructor for the class can take up to four parameters:
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript" src="alert-mini-latest.js"></script>
<script type="text/javascript">
new Alert("Here's an example of what I'm talking about.");
</script>
Options
Here's an example using all the possible options. Obviously, you can't use the top, bottom, left, right and button options all at the same time, because that doesn't make sense.
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript" src="alert-mini-latest.js"></script>
<script type="text/javascript">
new Alert(
'This is the popup in action.', {
width: 200,
border: '1px solid red',
padding: 10,
background: '#FFF',
speed: 400,
wait: 3000,
buttonType: 'confirm',
buttons: [{
text: 'Text for button 1',
title: 'Title of button 1',
classes: 'confirm-button'
}, {
text: 'Text for button 2',
title: 'Title of button 2',
classes: 'cancel-button'
}],
position: {
center: true,
type: 'fixed',
top: 100,
right: 100,
bottom: 100,
left: 100
}, function() {
new Alert('This is the callback for button 1.');
}, function() {
new Alert('This is the callback for button 2.');
}
}
);
</script>
Here's a more sensible example: click me
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript" src="alert-mini-latest.js"></script>
<script type="text/javascript">
$('.new-popup').bind('click', function() {
new Alert(
'This is the popup in action. This one features a close button.', {
width: 200,
border: '1px solid #999',
padding: 15,
background: '#EEE',
speed: 400,
buttonType: 'close',
position: {
top: 50,
right: 50
}
}
);
});
</script>
Here's an example with buttons and callback functions: click me
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript" src="alert-mini-latest.js"></script>
<script type="text/javascript">
$('.confirm-popup').bind('click', function() {
new Alert(
'Please confirm if you really want to do this.', {
buttonType: 'confirm'
}, confirm, cancel
);
});
//Do this when someone click confirm
function confirm() {
new Alert('You just clicked button 1!');
}
//Do this when someone clicks cancel
function cancel() {
new Alert('You just clicked button 2!');
}
</script>
Download
You can download a minified version of the code from http://downloads.jjok.co.uk/javascript/alert-mini-latest.js.
Styling with CSS
The look of the Alert can be completely styled using CSS.
Here's an example: click me
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript" src="alert-mini-latest.js"></script>
<script type="text/javascript">
$('.css-popup').bind('click', function() {
new Alert(
'Here is my message. Here is my message. Here is my message. Here is my message. Here is my message.',
{button:true}
);
});
</script>
<style type="text/css">
.alert {
-webkit-border-radius:4px;
-moz-border-radius:4px;
border-radius:4px;
-moz-box-shadow:5px 5px 10px #CCC;
-webkit-box-shadow:5px 5px 10px #CCC;
box-shadow:5px 5px 10px #CCC;
background:rgba(240, 240, 240, 0.6);
padding-right:16px !important;
}
.alert .close {
overflow:hidden;
padding:0 0 0 8px;
width:0;
height:8px;
top:4px !important;
right:4px !important;
border:1px solid #666;
background:#FCC;
}
.alert .close:hover {
background:#F33;
}
</style>
Notes
The height of the box cannot be specified in the options. It is worked out automatically, depending on the size of the content.
The library has been tested for compatibility with the following browers:
- Internet Explorer 7+
- Opera 10+
- Firefox 3.5+
- Chrome 4+
- Safari 4+
ChangeLog
-
Version 1.12 (2010/10/21)
- Added window resize listener to recalculate the position of the popup when the browser window is resized.
-
Version 1.11 (2010/08/25)
- Bug fix for Internet Explorer 7.
-
Version 1.10 (2010/07/14)
- Changed 'button' option to 'buttonType', so a single 'close' button, or two 'confirm' and 'cancel' buttons, can be shown.
- Added third and forth arguments to the constructor. These are functions to be called when buttons are clicked.
-
Version 1.02 (2010/05/18) docs
- Added ID to options.
- Bug fixes.
-
Version 1.00 (2010/04/09)
- Initial release.