In a partial view I load a javascript file like this :
<script src="@Url.Content("~/Scripts/Testing.js")" type="text/javascript"></script>
In the Testing.js, there are some functons defined
That’s work, the functions defined in the file can be used.
Now I do an action in my application, my controller return an another (where I don’t load any js file) partial view. The functions defined in Testing.js are still available in this partial view.
Is there a way to “remove” the functions loaded in the first view ?
Thanks,
Update 1
I tried this in the partial view but error :
The file “~/Views/xxxx.cshtml” cannot be requested directly because it calls the “RenderSection” method.
@section MyScript {
<script src="@Url.Content("~/Scripts/Testing.js")" type="text/javascript"></script>
}
@RenderSection("MyScript", false)
You should avoid referencing any scripts in partials. You could define a section in your Layout, for example just before the closing
</body>which will allow for views to include some custom scripts:and then in the view (not in the partial view) override this section to include any scripts that this view might need: