Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an asp.net-mvc website where there is a top section with a bunch of filter information and the middle section is a reports. I now have a few different report formats and I want to toggle between a few reports. I have it working by making them all partial views and loading them via ajax (to avoid loading the common info over and over again) but one issue i realized is that some of the different reports have different javascript that goes along with them. For now, I am loading up all of the javascript files in the main parent page but I realized that I am wasting a lot of resources by download and wiring up all of the jquery events even if i never actually view a report

Is there anyway I can pass some javascript along with downloading a partial view in asp.net-mvc so I only load this and wire up the events "on demand" as required (instead of always)

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Of course you can. Just be aware that the effects of the code will stick around even if you later remove the code itself: any functions you defined will remain defined, any listeners you attached will remain attached (as long as their target elements persist)... so it would be a good idea to make a setup() and teardown() methods for the loading code, that you'd invoke from your controlling code, rather than just drop a bunch of code to execute as it loads.

However, I would say it would need to be a rather unique set of circumstances for me to employ this method; for most part, it would be much easier and efficient to just load all the code you need at once, to benefit from client caching if nothing else. Toggle the behaviour of your code, don't toggle the code.

share|improve this answer
    
thanks... if you take that to the extreme though, lets say you have an app that is a single page and the entire lifecycle is swapping partial views . . at some point loading up everything at the beginning in the parent page seems inefficient. Also you mention "of course you can" but didn't explain how you would go about it . –  leora Apr 14 '14 at 1:11
    
The "how" is the same as any javascript loading: include a <script> tag. It "seems" inefficient, but it is not, unless you have a huuuuge codebase. Caching makes a big difference, since the code is only ever loaded once, and in a single connection; splintered, the code would either be uncached (if inside the <script> tag) or open additional connections (if linked from the <script> tag). As always, YMMV, and you can't say anything with certainty unless you profile it on your own use case. –  Amadan Apr 14 '14 at 1:42
    
but i thought the script tag had to be in the head section of a page –  leora Apr 14 '14 at 1:58
    
Nope, it can go pretty much anywhere (by spec, anywhere a metadata, script or phrasing elements can go: for example, it can go wherever <b> or <textarea> can). If in head, then you're sure it will be run before any body is received. A common alternative to onload or various ready handlers is to place the script at the very bottom of the <body> tag, which ensures that the whole (main) document is loaded before the script is executed. –  Amadan Apr 14 '14 at 2:03

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.