Destroy tag javascript

Asked

Viewed 82 times

1

I have an Asp.Net MVC project structured as follows:

On the page Home there are several links to others Actions and Controllers, and they return a PartialView that assembles all the content into one modal.
So far so good, the problem is that when closing a modal of this type, your scripts remain "alive" somehow. Therefore, when opening a new modal that has the same elements of the previously opened modal, the scripts of the closed modal are fired in the new modal.

Is there any way to "kill" these scripts definitely?
Maybe by id of tag <script id="scriptZumbi">.

  • I think maybe that’s not the way. It’s kind of a gambit. If you can handle the script and modals I suggest seven a class for each modal and direct the script to each class separately according to what you want. Or put the scripts inside each partialview. Maybe if you post some code I can help better.

  • Is this modal generated within a div or something? Remove it from your code, it also makes the page lighter.

1 answer

2


No. Everything indicates that your code is full of bad practices.

First of all, a Partial should not have Javascript code inside it. The code should be all in the View Father, just to avoid this kind of behavior.

Secondly, it is not a good idea to "destroy" HTML code, because HTML was not designed to have within it a state machine, which is what you are proposing to do. There are frameworks that work like this, but the entire application cycle is managed by framework in itself, then possibly you would have to reimplementate the whole system following the guidelines of this framework.

Third and last, if the goal is to have a system behaving more or less like a SPA (Single Page Application), it would be better to quit ASP.NET MVC and go to the Web API. It would even make more sense from the point of view of client behavior with the server part.

  • Well, I believe you were one of the few to understand my problem. Thank you for the answer. Really my code is full of bad practices. I’m just doing system maintenance and I realized that there are Javascript codes in Partialview that when they are assembled and then dispersed their code remains "alive" and interfering in other Partialviews.

  • Then migrate them to the Views father. Many of these problems will disappear.

  • The problem is that there are elements in a Partialview with the same id of another Partialview, but events can not be the same.

  • 1

    Then manage Ids when opening the Partial, or simply don’t use Ids. Use classes.

Browser other questions tagged

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