[javascript] 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

This question is related to javascript html asp.net-mvc

The answer is


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>

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to asp.net-mvc

Using Lato fonts in my css (@font-face) Better solution without exluding fields from Binding Vue.js get selected option on @change You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to send json data in POST request using C# VS 2017 Metadata file '.dll could not be found The default XML namespace of the project must be the MSBuild XML namespace How to create roles in ASP.NET Core and assign them to users? The model item passed into the dictionary is of type .. but this dictionary requires a model item of type How to use npm with ASP.NET Core