Comments (2)
minified script in downloads sections is old and buggy, you need take correct
script from svn
Original comment by [email protected]
on 24 Jan 2011 at 8:31
from jsload.
This is not related to whether you grabbed the file from Downloads or SVN -- as
far as I can tell the SVN version fail as well and will never respect the
"path" property other than when set in load, as in:
var jsLoader = new JSLoad(tags, "/somedir/");
But if overridden in the model:
{
name: 'swfobject',
// i wanna load swfobject from google
path: 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/'
}
the "path" property will never be used (though the implied usage is just that).
It appears in the source code that the undocumented usage was supposed to be
that for external files you would put the full URL in the "name" property
rather than split it between the "name" and "path" properties. You can see this
on line 134:
if ( tagName.indexOf("http://") > -1 ) {
var filePath = tagName;
} else {
var filePath = (path ? path : "") + tagName + '.js';
}
Notice that the code checks the "tagName", and if it (*not* the "path"
property) has "http://", it accepts the entire string as the filePath
(including not adding the ".js"); otherwise, it checks for the path (and does
add the ".js").
So you could change your code as follows to make it work:
{
name: 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
}
and then call it using the "load" method:
jsLoader.load(['http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
]);
This will fail if are using a https protocol, which a lot of the Google CDN
files are. Anyway, it's strange to have a "path" property that is ignored for
external loads, and this is verbose.
One solution is to fix the logic to check for all valid URL protocols. So you
could change the logic on line 134 to:
if (string.match(/^(http:\/\/|https:\/\/|\/\/)/gi) !== null) {
which now checks for http:// || https:// || // (the latter is common) at the
start of the tagName.
But this won't solve all your problems. Suppose this initialization:
var tags = [
{
name: 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'
},
{
name: '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'
},
{
name: 'test'
},
{
name: 'mustache',
path: '/loaders/public/javascripts/'
}
];
var jsLoader = new JSLoad(tags);
jsLoader.load(['https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js', 'mustache', 'test']);
First, mustache.js will not load because the "path" property is ignored. The
code looks for what I believe is a global "path" property (set as part of the
JSLoad instantiation), instead of the tag.path. Line 137 is:
var filePath = (path ? path : "") + tagName + '.js';
but I believe it needs to be:
var filePath = (tag.path ? tag.path : "") + tagName + '.js';
So the finished rewrite of lines 134 - 138 is now:
if (tagName.match(/^(http:\/\/|https:\/\/|\/\/)/gi) !== null) {
var filePath = tagName;
} else {
var filePath = (tag.path ? tag.path : "") + tagName + '.js';
}
This works, but it keeps the ".js" / no ".js" issue (easily fixed but enough
already).
So...I think there is a simpler and better change that will always use the
"path" property, and if it is missing ("undefined") it will default to the
current directory (by way of the path becoming ""). To do that, remove lines
134 - 138 and replace them with:
var filePath = (tag.path ? tag.path : "") + tagName + '.js';
This seems to vastly simplify the logic: if there is a "path" property set, use
that, no matter http or https or // or /somdir/; if path is undefined, use the
current directory (by use of an empty string). And the ".js" is always added so
we never change our approach. The bonus is, your model is consistent and
cleaner:
var tags = [
{
name: 'jquery.min',
path: 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/'
},
{
name: 'jquery-ui.min',
path: '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/'
},
{
name: 'test'
},
{
name: 'mustache',
path: '/loaders/public/javascripts/'
}
];
var jsLoader = new JSLoad(tags);
jsLoader.load(['jquery.min', 'jquery-ui.min', 'mustache', 'test']);
Sorry for the long, convoluted answer. There may be reasons my change(s) don't
make sense. I haven't tested beyond what I've shown here. I was just testing
out various loading libraries and had high hopes for this, but it seems no one
cares much about it anymore so this will be the extent of my troubleshooting.
Original comment by [email protected]
on 9 Jan 2012 at 6:28
from jsload.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsload.