I read http://ajaxpatterns.org/On-Demand_Javascript and got interested in “lazy loading” my JS. Questions:
- Can anyone recommend a good plugin for this?
- Any “real world” advice implementing such a strategy? Any “gotchas” I should look out for?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No plugin needed. You can use jQuery’s
$.getScript(). Just put a particular event’s javascript in a separate file, then bind an event that calls$.getScript().This will ensure that you never load more javascript than you need to. If the user never clicks on the element, you never loaded the javascript for the event. There will be a small delay for the HTTP request, so you should probably indicate a loading animation on click while the script loads.