dailinjie / jsload

Automatically exported from code.google.com/p/jsload
0 stars 0 forks source link

Cannot Load External Scripts #4

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
I've attached my setup of JSLoad and a screenshot of the output. if you dont 
mind taking a look. 
maybe i did something wrong.

What is the expected output? What do you see instead?
I want to load external scripts from Google AJAX Library. i've added the path 
configuration, but 
JSLoad still loads the version on my server.

What version of the product are you using? On what operating system?
i'm using JSLoad 0.9, i got it directly from the Downloads section. and i'm 
currently developing on 
Safari 4.0.5 and Firefox 3.6.2.

thx,
Rizky

Original issue reported on code.google.com by br4inwash3r on 12 Apr 2010 at 9:51

Attachments:

GoogleCodeExporter commented 8 years ago
minified script in downloads sections is old and buggy, you need take correct 
script from svn

Original comment by ramunas....@gmail.com on 24 Jan 2011 at 8:31

GoogleCodeExporter commented 8 years ago
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 jklegs...@gmail.com on 9 Jan 2012 at 6:28