Load external scripts in modal being loaded via ajax

Asked

Viewed 697 times

2

By default, I have a configuration file of various plugins and actions of my system

A simple example is the use of tooltip:

$(".bn-tooltip").tooltip({
    track: true
});

Datepicker:

$('.bn-datepicker').datepicker({
    autoclose: true,
});

I do this because when I will use anywhere in the system, I just call the class I have set there.

Well, it turns out that when I press I see ajax my modals, it does not recognize these "settings" and I have to pass inside the modal page the same codes I already have, or put the file config.js in modal...

Is there any elegant solution to not need to insert every time you use modal the same codes or always insert the files script?

  • I don’t quite understand, but it seems that the problem is you trying to apply the plugins to page elements before they exist. If so, no need to include scripts again, just initialize the plugins at the right time for each element.

1 answer

1


The elegant way is by putting a section for Scripts in the Modal Layout:

@RenderSection("scripts", required: false)

Thus, for each file of View other than a Partial, just call the Views with the following statement at the end of the:

@section scripts 
{
    <script>
        $(".bn-tooltip").tooltip({
            track: true
        });

        $('.bn-datepicker').datepicker({
            autoclose: true,
        });
    </script>
}
  • but my problem is when are partial views :/

  • I won’t even put it as an answer, but you can tag <script> within the Partial. It’s not elegant at all, but it works.

  • why even I asked the question, I do it in 2 ways, or the <script tag with the code inside, or with src for an external file

  • In this case the external file is a good.

  • from the evils the worst kkk

Browser other questions tagged

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