Send message to browser

Asked

Viewed 1,264 times

0

I need to send a message like this to the browser:

Exemplo

How can I do on a site made with C#, ASP.NET and Webforms?

  • Good afternoon, aspx and c# do not do front-end operations, this is javascript. Still you can control some situations of javascript that will be delivered to the front-end. I just don’t understand if it is a simple alert that you want or if you want to block the user from leaving the page without confirming, for example the user close the window by accident and you want it to confirm?

  • I just want to send an alert anyway, no events like leaving the page, if ASPX does not do it by itself, have to use javascript in it?

  • 1

    Maybe you want a link: <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick = "alert('Olá mundo!');"/>?

  • I corrected your question so that it has greater acceptance. It would be interesting to give some more details, if you use ScriptManager, for example, which is used to activate the JS on the page.

  • Exactly, thank you very much, I’m starting ASP.NET etc. Thank you so much for your help.

  • @Gypsy omorrisonmendez Is it worth I turn in response?

  • 1

    I think it’s worth.

  • Go with everything I give reputation

  • @Guilhermenascimento Basically, it is this translation here: http://stackoverflow.com/questions/10311341/confirmation-before-closing-of-tab-browser

  • @Could you see if I wrote any nonsense? I have no experience with .net. Thank you

  • @user28411 Thanks, good studies!

Show 6 more comments

2 answers

3


If you are creating a basic page or are studying the principle would be to create a link to trigger the event (not necessarily needing ASP.NET) in html:

<a href="javascript:void(0);" onclick="alert('Olá mundo!')">Clique aqui</a>

You can create a simple button as well:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="alert('Olá mundo!');"/>

Note that the asp:Button is used to control server-side events (which is accompanied by a <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 files .js, as an example:

<script type="text/javascript" src="foo.js"></script>

In foo.js will contain:

alert('Olá mundo!');

I believe that here the principle is more the issue of "html", but if you are interested (and I’m sure that if in the future you want to learn more about the advanced controls of .net), I recommend that you follow the Gypsy tip and read about the ScriptManager, follows link from the documentation:

The main types of dialog windows on web pages

  • The window.alert or simply alert, is an event that displays a simple dialog window with a text message (and does no 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 dialog window with a text message and two buttons, one to confirm an action and one to cancel the action (note that clicking close will be the same as clicking cancel), this event returns a boolean value, being true click on Ok or false click on 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 return a string, if you click cancel (or close) 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, chance has not return it will act normally by closing the window and ignoring other events, while using return on it you will be adding a check to the user if he wants to leave a particular page, this event is triggered in Passwords, sending form, reload the page, when you close a tab or window, example as quoted by Gypsy of this response from 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 modals, but the types of dialogs are basically those cited here, if you want to see more methods used by the object window, you can have a look at the Mozilla documentation:

  • Supplement your answer with the first response of this link. You didn’t say the most important thing.

  • @Ciganomorrisonmendez I did not put this answer, because it seemed to me that the AP asked about alert even though he used a drawing on the onbeforeunload. Note that he commented on this: http://answall.com/questions/91495/enviar-messaging para-navigator/#comment185267_91495 - But if it seems pertinent I will edit. Thank you

  • It’s not a simple dialog: is the dialog of unload.

  • @But this is what I said, he may have used such a drawing of the dialog of beforeunload, but he had commented that this was not the situation. It is those types of situation that is used to illustrate something, but what is desired is something else. I do not know what I understood. But I believe it is worth citing all dialogues windows in the reply. Ending my edition... grateful.

  • Perfect. Exactly that. + 1

1

You can use the onbeforeunload javascript, it presents this 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;
};

Browser other questions tagged

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