Skip to content

Instantly share code, notes, and snippets.

@lu1s
Created June 27, 2011 07:18

Revisions

  1. @pulidoman pulidoman created this gist Jun 27, 2011.
    50 changes: 50 additions & 0 deletions jquery-alert-hack.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    /*
    * This function replaces the javascript native alert() function.
    * I know that by convention it's not a good practice, but if you want to
    * follow strict stuff, just declare the function with another name and
    * call it as that.
    *
    * jQuery.js is required (http://jquery.com)
    *
    * The function can be called like this:
    * For static (not auto-hide) alert bar:
    * alert("my message <span>that supports html too</span>")
    *
    * For auto-hide after certain time:
    * alert("the message",true) // for automatic timeout (2000 by default)
    * alert("a messageee",4000) // with custom timeout in ms
    *
    */
    alert = function(what,auto){
    // if the element hasn't been appended to the body element
    if($("#alert_bar").length==0){
    // customize the bar with css
    var bar = $("<div/>").css({
    'display':'none',
    'position':'fixed',
    'top':'0px',
    'left':'0px',
    'zIndex':'50',
    'width':'100%',
    'margin':'0px',
    'padding':'5px auto 9px auto',
    'textAlign':'center',
    'fontSize':'14px',
    'background':'#eee',
    'color':'black',
    'borderBottom':'solid 1px #333'
    })
    bar.attr("id","alert_bar")
    $('body').prepend(bar)
    }
    // close button
    $("#alert_bar").html('<div style="text-align:right;font-size:10px;color:red"><span style="cursor:pointer" onclick="$(\'#alert_bar\').slideUp()">[cerrar]</span></div><div>'+what+'</div>')
    $("#alert_bar").slideDown()
    if(auto){
    if(typeof auto == 'boolean')
    auto = 2000
    window.setTimeout(function(){
    $("#alert_bar").slideUp()
    },auto)
    }
    }