How to call a javascript function on pageload in MVC

13,370

You can also use plain JavaScript as follows:

window.onload = function() {
  init();
  doSomethingElse();
};
Share:
13,370
Sachin Yadav
Author by

Sachin Yadav

Updated on June 04, 2022

Comments

  • Sachin Yadav
    Sachin Yadav almost 2 years

    This is my Java Script function:-

        function ValidatefuneralDate() {
        var today;
        var currentDate = new Date();
        var year = currentDate.getFullYear();
        var month = currentDate.getMonth() + 1;  
        var day = currentDate.getDate();
    
        if (window.location.href.indexOf("en-US") > -1) {
            today = month + '/' + day + '/' + year;
            var selectedDate = $("#Month option:selected").
            val() + '/' + $("#Day option:selected").text() + 
            '/' + $("#Year option:selected").text();
        }
        else {
            today = day + '/' + month + '/' + year;
            var selectedDate = $("#Day option:selected").
            text() + '/' + $("#Month option:selected").
            val() + '/' + $("#Year option:selected").text();
        }
        if ($("#Month option:selected").val()==2)
        {
            $("#Day option[value='31']").hide();
            $("#Day option[value='30']").hide();
            if ($("#Year option:selected").text() == 2016) {
                $("#Day option[value='29']").show();
            }
            else
            {
                $("#Day option[value='29']").hide();
            }
        }
        else if($("#Month option:selected").
        val() == 4 || $("#Month option:selected").
        val() == 6 || $("#Month option:selected").
        val() == 9 || $("#Month option:selected").val() == 11)
        {
            $("#Day option[value='29']").show();
            $("#Day option[value='30']").show();
            $("#Day option[value='31']").hide();
        }
        else
        {
            $("#Day option[value='31']").show();
        }
        if (selectedDate < today) {
            $("#lblFeneralDate").
            html('@Html.R(VirtualPath,"ValidatefuneralDate")');
            return false;
        }
        else { return true;}
      }
    

    How can i call this function on page load time on browser? This function should call at the time of Page Load how can i do this? how can a function bind with the page load?

  • Sachin Yadav
    Sachin Yadav about 9 years
    Please tell me how ? i dont know syntax
  • Sachin Yadav
    Sachin Yadav about 9 years
    But sir it gives the fixed value in Day DDL its not changing according to month
  • Sachin Yadav
    Sachin Yadav about 9 years
    its working but here only 2nd if condition is running control is not going to else part
  • Frebin Francis
    Frebin Francis about 9 years
    you question doesn't need answer for this question. right? please update your question first or post another question.
  • DavidG
    DavidG about 9 years
    That's a bug with your code, either fix that yourself or open another question.