最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - UnloadRemove javascript functions from ASP.NET MVC partial view - Stack Overflow

programmeradmin3浏览0评论

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)

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)
Share Improve this question edited Jul 26, 2012 at 8:08 TheBoubou asked Jul 26, 2012 at 7:38 TheBoubouTheBoubou 20k54 gold badges151 silver badges249 bronze badges 5
  • 2 Why are you loading javascript in a partial in the first place? Javascript should be loaded only once in your Layout. There must be something wrong with your design. What are you trying to achieve (and don't answer me please: I want to remove functions loaded in the first view)? – Darin Dimitrov Commented Jul 26, 2012 at 7:41
  • 1 I've faced the same problem. The solution is what @DarinDimitrov has suggested. Another problem with your approach is that if you load the partial view again, you'll have multiple scripts (same) in your page. – Sang Suantak Commented Jul 26, 2012 at 7:43
  • @DarinDimitrov in the first view the JS loaded is specific for this partial view and not needed in other. Ok, I can load in the layout. But the script can be huge at the end. And I don't see when I look the source code via the browser the JS function called in the partial view, for security reason is not better ? – TheBoubou Commented Jul 26, 2012 at 7:46
  • When I meant put it in the Layout, I didn't mean literary stuffing your entire script inline there. I meant that you should use an external javascript file that you should reference. And you should reference it in a scripts section from the view so that this script is included only for views that need it, not all of them. As far as the security reasons that you are pointing out, in both situations there strictly 0 security. In both cases javascript is pletely accessible to the client. So don't think about any security if you are doing javascript as there's none. – Darin Dimitrov Commented Jul 26, 2012 at 7:48
  • The RenderSection call should be done in your Layout, not in your view. What's the point of defining a section in the view and rendering it immediately after in this view? – Darin Dimitrov Commented Jul 26, 2012 at 9:03
Add a ment  | 

1 Answer 1

Reset to default 6

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:

    <script type="text/javascript" src="@Url.Content("~/scripts/some_mon_script_that_will_be_used_by_all_views_such_as_jquery_for_example")"></script>
    @RenderSection("scripts", false)
</body>
</html>

and then in the view (not in the partial view) override this section to include any scripts that this view might need:

@section scripts {
    <script src="@Url.Content("~/Scripts/Testing.js")" type="text/javascript"></script>
}
发布评论

评论列表(0)

  1. 暂无评论