0

I have made a fully AJAX-based website that gets a snapshot when you visit the url, but loads the future contents with AJAX. Some pages, however, needs additional JS-files. Therefore I'm thinking about making a dynamic loader. (The main reason why I use AJAX is speed...)

Do I need to unload unneeded JS-files? Will the dynamic loading slow down the website if I just keep adding JS-files without unloading unneeded ones?

6
  • 1
    If they're not needed, why are you loading them in the first place? Do you mean, not needed anymore? Commented Nov 18, 2012 at 18:43
  • 1
    It looks like you need to stop using AJAX until you learn why you should use ajax... Ask a professor at Hogwarts... Commented Nov 18, 2012 at 18:44
  • @ŠimeVidas Yes, not needed anymore. Commented Nov 18, 2012 at 18:44
  • @gdoron I want to use AJAX because it eliminates the need for loading the same contents over and over again. (The stuff above and below the contents in the HTML-file.) Commented Nov 18, 2012 at 18:45
  • @StudentofHogwarts. Well I'm not getting why you need to use ajax for not loading stuff "over and over again", but if you enjoy using AJAX, enjoy!!! Commented Nov 18, 2012 at 18:46

2 Answers 2

1

Generally, no. But there might be exceptions based on what the scripts are doing. For instance, if one of the scripts sets an interval, or binds an event handler, then you might want to turn those things off, once you don't need them anymore.

On the other hand, if the scripts are merely adding API to the page (e.g. jQuery plug-ins), then you don't have to worry about them. Just make sure to not load them multiple times.

Sign up to request clarification or add additional context in comments.

Comments

0

The code loaded in a tag is not removed from memory if you remove the tag from the DOM. So that shouldn't be your motivation. And there are studies that show that it's not a good idea to keep those tags in the DOM for a long time (I'm sorry but I can't find the source right now). So you may want to remove them.

However, don't write your own script loader. There are plenty of JS loaders out there that are great. Here are two that are based on AMD modules, a way to organize your code into modules:

  1. RequireJS: http://requirejs.org/
  2. Curl: http://github.com/cujojs/curl

Here's another one that just loads scripts:

  1. LABjs: http://labjs.com/

And finally the YUI library also loads JS files, has its own module system and also helps with loading parts of webpages via Ajax:

1: YUI Library: http://yuilibrary.com/

4 Comments

Dynamic script loading does not necessitate a <script> element. For instance, jQuery's $.getScript() loads the code via Ajax, and then executes it directly via an indirect eval() call. (So there won't be any additional <script> elements in the DOM.)
Getting script via Ajax is ok if you only need to get them from the same domain of your webpage, but it won't work for remote scripts. And as a site grows, it's always very likely it'll need to load scripts from different domains being static file servers, CDNs, etc. So it's not something I'd rely on.
What is the benefit of using one of those script loaders instead of making my own? It's only 50 lines of code..
They deal with cross-browser issues and other subtleties. Additionally, structuring your code into modules is a great way of maintaining a JavaScript codebase, which if you're loading everything dynamically can become quite extensive. But more to the point, the "load portions of the page dynamically" problem is a big problem but it's been studied. So developing on top of infrastructure built with that problem in mind is a good idea. Here's another example that addresses loading stuff via Ajax based on the URL: yuilibrary.com/yui/docs/pjax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.