What is the good solution to replace Alert?

Asked

Viewed 1,252 times

2

In a question, how to do the alert, has a response, the most voted, that says:

Alert is not a good solution to show some message to the user in the browser but I will answer what you asked...

What would be the good solution to show some message to the user then?

  • A modal that resembles an Alert, because Alert is kind of "bland" and besides being defined by the browser its design. Modal type example: http://plnkr.co/edit/NePR0BQf3VmKtuMmhVR7?p=preview

  • Look, there are N tools to generate "most beautiful" Leds, such as Sweetalert. The problem with Alert, in my opinion, is that it is ugly to the end user.

  • Whenever someone says that the simplest solution of all, native to the language and universally implemented in all browsers is not a good solution, suggest them to look for a psychologist. She probably suffers from Not-Invented-Here Syndrome.

2 answers

6


The message should be displayed on the page itself, preferably close to the data that originated the error, highlighting, perhaps temporarily and in a way that does not obstruct the normal use of the page. And that was the spirit of the answer, I can assure you :)

Mensagem de erro

Of course there are exceptions. There may be some case that opening a default browser alert template may be more appropriate. But this is a practice considered old and unfriendly.

Even in this case where a modal is the most indicated today it is considered more presentable to make a window inside the page with a look consistent with the rest of the page.

inserir a descrição da imagem aqui

PHP is a technology of backend then it makes little difference how it is presented to the user. In frontend you can create a user experience however you want. This is done with Javascript, HTML, CSS. Note that the original question was speaking in Javascript, not PHP. PHP does not even know what is running in the client.

There is an infinite amount of ways to do this, researching here at Sopt already find several (here and here).

0

it is possible to create Javascript by php, but it will not necessarily be created by php, because php is not executed on the client machine, but on the server generating the HTML (js and css) code for the client

<?php
   echo "<script>alert('bem vindo ao site');</script>";
?>

it is not very recommended to pass a message through Alert, that is the user himself can disable Alert, so it may happen that he does not receive your message the second time, a way to circumvent it would be a hidden part in the page with the message, example a div

<div id="kodomsg" style="display: none">
senha invalida
</div> 

when you need to display that message simply display it, you can use JS to do it dynamically

<script>
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.style.display = "block"; //"none" para ocultar
</script>

Voce can create a function to display/hide and call it for a specific event, such as pressing a button

<div id="kodomsg" style="display: none">
era uma vez um botão ... fim da historia
</div> 
<input type="button" value="aperte" onclick="kodofun()">
<script>
function kodofun(){
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.style.display = "block"; //"none" para ocultar
}
</script>

not necessarily need to be statico Voce can change the own message dynamically by JS too

<div id="kodomsg" style="display: none">
senha invalida
</div>
<script>
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.innerHTML = "essa é a nova msg";
</script>

Voce can even create something similar to Alert, let the floating div by overwriting the page with a button to close it using js and css, even can customize it however you want

<style>
#kodomsg{
    background-color: blue;
    width: 200px;
    height: 60px;
    position: absolute;
    left: 20%;
    top: 20%;
}
</style>

<div id="kodomsg">
bem vindo ao site<br>
<input type="button" value="fechar" onclick="kodofechar()">
</div> 

<script>
function kodofechar(){
   var minhamsg = document.getElementById("kodomsg");
   minhamsg.style.display = "none";
}
</script>

inserir a descrição da imagem aqui

has many interesting ways of doing this as well as there are many applications for it (ps: this example I get ugly for a lot kkk)

Browser other questions tagged

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