I need to send a message like this to the browser:
How can I make a site made with C #, ASP.NET and Webforms?
If you are creating a basic page or are studying the principle would be to create a link to trigger the event (not needing ASP.NET necessarily) in html:
<a href="javascript:void(0);" onclick="alert('Olá mundo!')">Clique aqui</a>
You can create a simple button too:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="alert('Olá mundo!');"/>
Note that asp:Button
is used to handle server side events (which is accompanied by <form>
)
To insert a simple javascript into an html page, the basics would be this:
<script type="text/javascript">
alert('Olá mundo!');
</script>
You can also use .js
files, for example:
<script type="text/javascript" src="foo.js"></script>
In foo.js
will contain:
alert('Olá mundo!');
I believe that here the beginning is more about the "html" question, but if you are interested (and I am sure that if in the future you want to learn more about .net advanced controls) I recommend following the Gypsy's tip and read about ScriptManager
, follow the documentation link:
The window.alert
or simply alert
, is an event that displays a simple dialog window with a text message (and does not do any more action), example:
function meuEvento() {
alert("Olá");
}
<button onclick="meuEvento();">Clique aqui</button>
The window.confirm
or simply confirm
, is an event that displays a dialogue window with a text message and two buttons, one to confirm one action and another to cancel the action (note that clicking on close will be the same as clicking cancel), this event returns a Boolean value, being true
if you click Ok
or false
if you click Cancel
(cancel):
function meuEvento() {
if (window.confirm("Você quer confirmar ou cancelar?")) {
alert("Você clicou em Ok");
} else {
alert("Você clicou em Cancelar ou em Fechar");
}
}
<button onclick="meuEvento();">Clique aqui</button>
The window.prompt
or simply prompt
, is an event that displays a dialog window with a text message, a text field and a button to confirm, and another to cancel. This method returns a string
, clicking cancel (or closing) will return a null
instead of string
:
function meuEvento() {
var resposta = window.prompt("Qual o seu nome?");
if (resposta !== null) {
alert("Seu nome é:" + resposta);
} else {
alert("Você clicou em Cancelar ou em Fechar");
}
}
<button onclick="meuEvento();">Clique aqui</button>
The event window.onbeforeunload
is what makes the "effect" of the image you posted, this event expects a return
, does not return
it will act normally closing the window and ignoring other events, when using return
in it you will be adding a check for the user if he wants to leave a certain page, this event is fired in page, form sending, reload page, closes a tab or window, example as quoted by CiganoMorrisonMendez of this answer to SOen :
window.onbeforeunload = function(e) {
var msg = "Quer mesmo sair desta página?";
e = e || window.event;
// Pro IE e Firefox anterior a versão 4
if (e) {
e.returnValue = msg;
}
// Para Safari e Chrome
return msg;
}
<button onclick="window.location.reload();">Recarregar página</button>
<br>
<a href="?">Páginar</a>
Note that there are other window events, such as popups and modal, but the types of dialogs are basically those mentioned here, if you want to see more methods used by the window
object, you can take a look at the Mozilla documentation: / p>
You can use onbeforeunload
of javascript, it presents this little window that asks if the user really wants to leave the page.
window.onbeforeunload = function(e){
e = e || window.event, msg = 'Você tem certeza que deseja sair dessa página?';
if (e) e.returnValue = msg;
return msg;
};