External Javascript

In the past, one could enter a URL to add an external javascript library (i.e. jQuery, Socket.io.js) to a Script Component. I cannot seem to do that now. Has this feature been removed, or maybe only available to subscribed accounts?

Hello,

You can no longer add an external URL. You can however create a new script and copy paste the content of the external script inside the new script.

That’s what I was afraid of…

Thank you @vaios :smile:

Oh, is this going to cause you problems, @h4v0c?

No this is fine @will, at this point I’m merely testing different functionality. But thank you for the concern :smile:

Oh, that’s good. :smile: Just to explain why we made the change… Some users were pasting the URL for the Editor into that URL box. It was hard to sanity check what people were entering there. And really, the script component should just be a picker and scripts need to be treated more like any other asset for consistency. Hope that explains it.

Ah, yes, I agree. In light of that, I would keep it as is.

Thank you for the quick replies @will :smile:

Does this mean that we can’t use jQuery (et al) from a CDN? Only by embedding a complete copy into the project?

You can do something like:

var script = document.createElement('script');
script.onload = function() {
  alert("Script loaded and ready");
};
script.src = "http://whatever.com/the/script.js";
document.getElementsByTagName('head')[0].appendChild(script);

But, to be honest, I would just add jQuery (or whatever) to your project.

1 Like

Thanks Will,

Yes, I thought about that afterwards. It’s an OK technique, apart from it’s slower as most browsers are good at parallel downloads from script tags and parse as they load…

It’s not so bad when it’s just one or two files, but some libraries are more than that, and I come from the school of never embed dependencies in a non-managed fashion. NPM integration? :wink:

Thanks for the answer though.

Another approach (if you have a Personal or Org account) is to download your app when you’re ready to publish and just edit the HTML. Or concatenate your library with the PlayCanvas engine or your game scripts JavaScript file. Once downloaded, you can do whatever you like to the generated files.

Thanks Will. Yes a post-process optimisation step might be the way to go. Food for thought!