Alert after page load
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>
With the use of jQuery to handle the document ready event,
<script type="text/javascript">
function onLoadAlert() {
alert('<%: TempData["Resultat"]%>');
}
$(document).ready(onLoadAlert);
</script>
Or, even simpler - put the <script>
at the end of body
, not in the head
.
If you can use jquery then you can put the alert inside the $(document).ready()
function. it would look something like this:
<script>
$(document).ready(function(){
alert('<%: TempData["Resultat"]%>');
});
</script>
To include jQuery, include the following in the <head>
tag of your code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
Here's a quick example in jsFiddle: http://jsfiddle.net/ChaseWest/3AaAx/