Jump to content

Making a script loader with jQuery


DWilliams

Recommended Posts

First things first I'm terrible with JavaScript so I'm muddling through almost everything I do with it :D

 

My basic goal here is that I need to "include" a list of JS files into my main file and pause execution of my main script until they're loaded. After some googling I've found jQuery's getScript() method. I tried to make a script loader function based on my (working) image preloader.

 

Here's my code:

function loadScripts(callback)
{
var scripts = new Array('cCell.js', 'cPlayer.js');

var loadedScripts = 0;	
var numScripts = scripts.length;

for(var i = 0;i < numScripts;i++)
{
	$.getScript(resPath + 'js/' + scripts[i], function() {
		if(++loadedScripts >= numScripts)
			callback();
	});
}
}

 

resPath is defined outside that function but I have checked to verify the paths it creates is a valid path to my script. No matter what I try, the callback never seems to get called.

 

At first I had resPath set to be a complete URL to my resource folder. I read somewhere that JS has security features to prevent executing code from a remote URL or something so I tried replacing it with both relative and absolute paths to no avail.

 

What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/253738-making-a-script-loader-with-jquery/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.