Student of Hogwarts
Student of Hogwarts

Reputation: 1138

JavaScript loading because of AJAX

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?

Upvotes: 0

Views: 59

Answers (2)

juandopazo
juandopazo

Reputation: 6329

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/

Upvotes: 0

Šime Vidas
Šime Vidas

Reputation: 185913

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.

Upvotes: 1

Related Questions