Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert after page load

This is my script :

<%
    if (TempData["Resultat"] != null){
%>
<script type="text/javascript">
    alert('<%: TempData["Resultat"]%>');
</script>
<%
    }
%>

In this case pop-up is shown before page loaded, but i want that's appear after page is fully loaded. in Html it's looks like this :

<body onload="happycode() ;">

but i can't use it in MVC i got one master page for all my web application

like image 228
Chlebta Avatar asked May 06 '12 22:05

Chlebta


People also ask

How do I show alerts after page load?

JavaScript Alert: After Page Loads If you prefer to have the full page visible while the user reads the alert box message, use the onLoad event. To do this, place a function in the document's <head> , then trigger it using the onLoad attribute in the document's <body> tag.

How do I make an alert pop up in HTML?

The Window alert() method is used to display an alert box. It displays a specified message along with an OK button and is generally used to make sure that the information comes through the user. It returns a string which represents the text to display in the alert box.

What is alert () in JavaScript?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

What is a alert box which function is use to display the same?

Alert Box. Use the alert() function to display a message to the user that requires their attention. This alert box will have the OK button to close the alert box. Example: JavaScript Alert Message. alert("This is an alert message box."


1 Answers

There are three ways.
The first is to put the script tag on the bottom of the page:

<body>
<!--Body content-->
<script type="text/javascript">
alert('<%: TempData["Resultat"]%>');
</script>
</body>

The second way is to create an onload event:

<head>
<script type="text/javascript">
window.onload = function(){//window.addEventListener('load',function(){...}); (for Netscape) and window.attachEvent('onload',function(){...}); (for IE and Opera) also work
    alert('<%: TempData["Resultat"]%>');
}
</script>
</head>

It will execute a function when the window loads.
Finally, the third way is to create a readystatechange event and check the current document.readystate:

<head>
<script type="text/javascript">
document.onreadystatechange = function(){//window.addEventListener('readystatechange',function(){...}); (for Netscape) and window.attachEvent('onreadystatechange',function(){...}); (for IE and Opera) also work
    if(document.readyState=='loaded' || document.readyState=='complete')
        alert('<%: TempData["Resultat"]%>');
}
</script>
</head>
like image 83
Danilo Valente Avatar answered Oct 02 '22 23:10

Danilo Valente