Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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
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 Jul 26 '12 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 Jul 26 '12 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 ? – Kris-I Jul 26 '12 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 completely accessible to the client. So don't think about any security if you are doing javascript as there's none. – Darin Dimitrov Jul 26 '12 at 7:48
    
@DarinDimitrov see update1 – Kris-I Jul 26 '12 at 8:07
up vote 6 down vote accepted

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_common_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>
}
share|improve this answer
1  
And if I have only some index pages and a lot of partial ? – Kris-I Jul 26 '12 at 12:12
1  
You include the script in every view (not partial) that is susceptible to include those partials. You could have a gazillion of partials included in a main view. It doesn't matter. Remember that the final product of your application is a single HTML page. That's what the browser sees. The browser doesn't know what a partial means. So stop thinking about partials. Think in terms of functionality that your application offers and include the script in each page that will require it. – Darin Dimitrov Jul 26 '12 at 12:13
    
Ok than, in my case, I need to load in the layout because the main screen has a menu on the left and on the right is a <div> where I load partial view. – Kris-I Jul 26 '12 at 18:03
    
You have 2 possibilities: either you use Ajax.* helpers and include the unobtrusive ajax script or you use standard helpers and write javascript to AJAXify them that you include in your views. – Darin Dimitrov Jul 26 '12 at 18:58
1  
@Shimmy, it is by design. Scripts don't belong to partial views. Simply register those scripts in the parent view hosting those partials. – Darin Dimitrov Nov 26 '12 at 12:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.