<html>
<head>
<meta name="description" content="Bootstrap Alert controller" />
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet" type="text/css" />
<script src="http://twitter.github.com/bootstrap/assets/js/bootstrap.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div class="container">
<form action="javascript:;" method="post" class="form-inline" id="form">
<input type="text" name="message" class="input-medium" placeholder="Message" value="Hello, world!" />
<input type="text" name="header" class="input-medium" placeholder="Header" />
<input type="text" name="style" class="input-medium" placeholder="Style, e.g. info, success, error" />
<input type="number" name="hidden" min="0" max="99999999" step="1000" class="input-medium" placeholder="Milliseconds to disappear" />
<label><input type="checkbox" name="scroll" value="1" />Scroll to the top?</label>
<br />
<button type="button" class="btn" data-action="insert">Show</button>
<button type="button" class="btn" data-action="clear">Clear</button>
</form>
</div>
</body>
</html>
input{font-size:small}
input[type=text],input[type=number]{height:2em}
/**
* message for bootstrap
*/
var message = (function ($) {
if (!$.isFunction($.fn.alert)) {
console.log('Twitter Bootstrap Alert is not loaded.');
return false;
}
var alert = $('#message').length ? $('#message') : $('<div id="message"></div>').prependTo('body');
var is_number = function (n) {
return !isNaN(parseFloat(n, 10)) && isFinite(n);
};
return {
/**
* Show alert
* @param $option {object}
* message {string} Alert Context
* header {string} Alert header
* style {string} Alert style (e.g. info, success, error, block)
* hidden {int|bool} Disappear after the milliseconds (false)
* scroll {bool} Scroll to the top? (true)
*/
show: function (option) {
// default setting
var def = {
message: null, // context
header: null, // header
style: null, // alert style
hidden: false, // disappear after seconds
scroll: false // scroll to first alert
};
// combine options
var args = $.extend(def, option);
var html = '<div class="alert hide fade in"><button type="button" class="close" data-dismiss="alert">×</button><strong></strong> <span></span></div>';
var clone = $(html).clone();
var ele = {
body: clone.find('span:first'),
header: clone.find('strong:first')
};
var id = new Date().getTime();
clone.attr('id', id);
// message must be given
if (args.message && 'string' === typeof args.message) {
ele.body.html(args.message);
if (args.header && 'string' === typeof args.header) {
ele.header.text(args.header);
}
if (args.style && 'string' === typeof args.style) {
clone.removeClass('alert-error alert-info alert-success alert-block').addClass('alert-' + args.style.replace('alert-', ''));
}
if (is_number(args.hidden)) {
if (0 < args.hidden) {
window.setTimeout(function () {
$('#' + id).alert('close');
}, parseInt(args.hidden, 10));
}
}
clone.show().alert().appendTo(alert);
// scroll to the top?
if (args.scroll) {
$('html,body').animate({
scrollTop: (alert.find('.alert:first').offset().top - 60)
});
}
}
},
/**
* close all alert
*/
close: function () {
alert.find('.alert').alert('close');
}
};
}(jQuery));
// serialize form
$.fn.serializeObject = function () {
var o = {}, a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
if ('' !== this.value) {
o[this.name].push(this.value);
}
} else {
if ('' !== this.value) {
o[this.name] = this.value;
}
}
});
return o;
};
// body onload
var onloading = (function () {
$('body').on('click', ':button', function () {
var action = $(this).data('action');
switch (action) {
case 'insert':
var param = $('#form').serializeObject();
message.show(param);
console.log(typeof message.show);
break;
case 'clear':
message.close();
break;
}
});
}());
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |