Personalize an Alert

Asked

Viewed 28,732 times

3

I have a code that by clicking the button it opens a new tab. Depending on the condition of the report it shows a Alert that by clicking on OK it closes the tab that was opened. Is there any way I can customize this Alert, to look more beautiful on my site?? My code is in codeigniter.

  • Yes, it is possible, this is more the javascript side than php.

2 answers

10

There is no way to personalize a alert, but you can use some modal, an excellent that I recommend is the Bootbox.js based on Bootstrap. Example:

bootbox.alert("Hello World");

$('#abrir').click(function(event){
  bootbox.alert('Oi, eu sou um alert.', function(){
    bootbox.alert('Oi eu sou um alert chamado apartir de um callback de outro alert.');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" integrity="sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>

<script src="https://github.com/makeusabrew/bootbox/releases/download/v4.4.0/bootbox.min.js"></script>


<div class="container">
  <br>
<button class="btn btn-primary" id="abrir">Abrir Alert</button>
  </div>

  • Only that there is a little problem, this Alert that I am creating it is in the Controller so you can see if there are any selected items.

  • Hmm can post your code @Ketlin?

  • If you can make a call to Alert you could also call the code @Kaduamaral posted. The principle would be the same, no?

3

You can create a html customized, looking like a modal, and create a function to show the same, as if it were a Alert.

An example would be like this:

Alert.render('Alert Diferente')
#dialogoverlay{
	display: none;
	opacity: .8;
	position: fixed;
	top: 0px;
	left: 0px;
	background: #FFF;
	width: 100%;
	z-index: 10;
}
#dialogbox{
	display: none;
	position: fixed;
	background: #000;
	border-radius:7px; 
	width:550px;
	z-index: 10;
}
#dialogbox > div{ background:#FFF; margin:8px; }
#dialogbox > div > #dialogboxhead{ background: #666; font-size:19px; padding:10px; color:#CCC; }
#dialogbox > div > #dialogboxbody{ background:#333; padding:20px; color:#FFF; }
#dialogbox > div > #dialogboxfoot{ background: #666; padding:10px; text-align:right; }
<script>
function CustomAlert(){
    this.render = function(dialog){
        var winW = window.innerWidth;
        var winH = window.innerHeight;
        var dialogoverlay = document.getElementById('dialogoverlay');
        var dialogbox = document.getElementById('dialogbox');
        dialogoverlay.style.display = "block";
        dialogoverlay.style.height = winH+"px";
        dialogbox.style.left = (winW/2) - (550 * .5)+"px";
        dialogbox.style.top = "100px";
        dialogbox.style.display = "block";
        document.getElementById('dialogboxhead').innerHTML = "Título";
        document.getElementById('dialogboxbody').innerHTML = dialog;
        document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()">OK</button>';
    }
	this.ok = function(){
		document.getElementById('dialogbox').style.display = "none";
		document.getElementById('dialogoverlay').style.display = "none";
	}
}
var Alert = new CustomAlert();
</script>
<div id="dialogoverlay"></div>
<div id="dialogbox">
  <div>
    <div id="dialogboxhead"></div>
    <div id="dialogboxbody"></div>
    <div id="dialogboxfoot"></div>
  </div>
</div>
<h2>Meus Alerts modificados</h2>
<button onclick="alert('Alert Normal')">Alert Normal</button>
<button onclick="Alert.render('Alert Modificado')">Custom Modificado</button>

This way, you can call the same as you call the Alert:

Alert.render('Alert Diferente')

Or at some event .

Credits: developphp.

JSFIDDLE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.