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
This question is related to
javascript
html
asp.net-mvc
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/
Add the code below in the PageLoad
Event:
ScriptManager.RegisterStartupScript(Page, this.GetType(), "myScript", "alert('OK Done.');", true);
$(window).on('load', function () {
alert('Alert after page load');
}
});
Another option to resolve issue described in OP which I encountered on recent bootcamp training is using window.setTimeout to wrap around the code which is bothersome. My understanding is that it delays the execution of the function for the specified time period (500ms in this case), allowing enough time for the page to load. So, for example:
<script type = "text/javascript">
window.setTimeout(function(){
alert("Hello World!");
}, 500);
</script>
Another option is to use the defer attribute on the script, but it's only appropriate for external scripts with a src attribute:
<script src = "exampleJsFile.js" defer> </script>
Why can't you use it in MVC?
Rather than using the body load method use jQuery and wait for the the document onready function to complete.
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
.
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>
Source: Stackoverflow.com