So, in my html file, I have a script for flashing message like this
<script>
$(document).ready( function() {
flashThisMessage("me");
});
//I want to move this code below to js file, so I dont have to copy this part
//to every html file that need to use, so I can just include the js file
(function($) {
$.fn.flash_message = function(options) {
options = $.extend({
text: 'Done',
time: 5000,
how: 'before',
class_name: ''
}, options);
return $(this).each(function() {
if( $(this).parent().find('.flash_message').get(0) )
return;
var message = $('<span />', {
'class': 'flash_message ' + options.class_name,
text: options.text
}).hide().fadeIn('fast');
$(this)[options.how](message);
message.delay(options.time).fadeOut('normal', function() {
$(this).remove();
});
});
};
})(jQuery);
</script>
and my js file look like this
function flashThisMessage( theMessage ) {
$('#status-message-area').flash_message({
text: theMessage,
how: 'append'
});
}
How can I move this the flash_message script into js file, I tried to just copy it, it will give an error "ReferenceError: jQuery is not defined" and "TypeError: $(...).flash_message is not a function" ?