Function $(Document). ready() only executes once

Asked

Viewed 2,863 times

1

I have an Asp.Net application that has a menu on a main screen that has 2 links, each to a page.

When I click on the first link and upload the first page, the function $(document).ready() wheel set and carries all the components I need.

When I click on the second link, analyzing step by step by Firebug, I notice the function $(document).ready() is not executed. Why ?

This application uses a layout page that is rendered before.

Function $(document).ready()

$(document).ready(function () {
    $('#spn').spinit({ min: 1, max: 100, stepInc: 1, pageInc: 10, height: 13, initValue: 95/*,callback: filtraModeloGrafico4*/});

    //Carrega o datepicker
    jQuery('#_InitialDateValue').datetimepicker( { format: 'd/m/Y H:i' } );
    jQuery('#_FinalDateValue').datetimepicker({ format: 'd/m/Y H:i' });

    jQuery('#_InitialDateValue2').datetimepicker({ format: 'd/m/Y H:i' });

    //Carrega a combo com os dias da semana
    $('#idComboTurno').multiselect({
        includeSelectAllOption: true,
        nonSelectedText: 'Select a week day',
        nSelectedText: 'days',
        numberDisplayed: 0
    });

});

The name of the file that has the function $(document).ready() is site.js

Page that loads all right:

@{
    ViewBag.Title = "Sequence Inspection - Graphics Report";
}

@Scripts.Render("~/Scripts/site.js")

<div class="row-fluid">
    <form class="form-inline">
        <div class="control-group span12">
         // Código...
         </div>
    </form>
</div>

Page that does not load Function $(document).ready()

@{
    ViewBag.Title = "Sequence Inspection - Failure Report";
}

@Scripts.Render("~/Scripts/site.js")

<div class="row-fluid">
@using (Html.BeginForm("ExportaExcel", "FailureReport", FormMethod.Post, new { @class = "form-inline" }))
{
  //Código HTML
}
</div>
  • 2

    Links are loaded by ajax, or are standard browser requests?

  • @bfavaretto, I load the links by the Asp.net helper for example: <li class="span3">@Html.ActionLink("Graphics Report", "GraphicsReport", "GraphicsReport")</li> <li class="span3">@Html.ActionLink("Failure Report", "FailureReport", "FailureReport")</li>

  • I will leave to someone who knows ASP.Net respond, I do not know what this helper generates, if it is linked to some js Microsoft, etc.

  • @bfavaretto Displaying the generated page source code we have <li class="span3"><a href="/GraphicsReport/GraphicsReport">Graphics Report</a></li>&#xA;<li class="span3"><a href="/FailureReport/FailureReport">Failure Report</a></li>

  • I imagined Emerson, but I don’t know if there’s any MVC script intercepting clicks on those links.

  • @bfavaretto The Helper generates a <form> in even pure HTML.

Show 1 more comment

1 answer

1


I don’t know if that’s the case, but JS inside Partial usually doesn’t run.

Another thing that influences is that you have not specified in which layout block this JS will be loaded. The correct is to specify @section scripts, which ensures that the JS will be loaded at the end of the entire layout rendering:

@section scripts {
    @Scripts.Render("~/Scripts/site.js")
}
  • Gypsy,followed his guidelines but had not yet worked. Before I had only one file .js responsible for loading the initializations of the two HTML pages I had. I then decided to create a file .js for each HTML page to be loaded. In each file .js, I put only the functions and initializations of components referring to their respective HTML page. I specified the @section scripts as you recommended. It worked and I noticed it got even faster. Thanks for the tips.

  • @Nothing Emerson ;) How great that everything worked out well. Needing, just shout.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.