I have a website whose navigation is all done via ajax (with jquery); each page is dynamically loaded into an element on the page. While I have a universal stylesheet and JS scripts, each page also has a page-specfic stylesheet and JS script. What is the most effect/efficient way to load these page-specfic scripts and stylesheets? On page load, most of the page-specfic scripts/stylesheets will not be needed. A page's scripts/stylesheet will only be needed when a user loads in (via ajax) a particular page. Should I load every single script and stylesheet at page load?
Another option would be to simply append appropriate <link>
and <script>
elements to the head when a page is dynamically loaded; however, would they be called? Also, would I need to remove the <link>
and <script>
elements when a different page is called (via ajax)? For the scripts, I could use jQuery's .getScript()
function. What is the best approach to this in terms of efficiency and cross-browser support?
Thank you!