No problem – until I then loaded a page that included this control AND also had its own requirement for jQuery. It seems that jQuery doesn’t like begin included twice, so I had a choice. I could:
- Include jQuery once in the header (or wherever) of each page (i.e. in the Master Page), hence breaking the self-containment of my control, or;
- Write a helper method to keep track of which script files had been included in each page and hence allow a script file to only be included once per request.
It should come as no surprise (as otherwise there would be no point in this post) that I went for the second option.
The HttpContext object has an Items collection, and since the HttpContext has a lifetime of one request, we can use this Items collection to keep track of our included files. Just add the following static method to a static class (Don’t you just love Extension Methods!):
It’s now just a matter of calling this from your .aspx pages, replacing the usual