Is it always safe to combine javascript files from different sources and load them as one?

I want to combine javascript files together and submit them as one from my site - so these are my code plugins and jquery or other third-party scripts. (I use Google CDN to host jQuery).

I was wondering that this is always guaranteed to be a safe thing. I am not an expert in Javascript, as things like namespacing go, and I was a little worried that it is possible to have something like a namespace construct that could cause a conflict. I am fine believing that javascript is all well-formed from every source.

As far as I know, the tag <script>essentially just inserts JS in place, as if it were in a file, but I wondered if there were any boundary cases where this is not the case.

I know that file concatenation is something that is common and used by javascript frameworks like Yahoo YUI , but it’s obvious that they have full control over their files.

+3
source share
3 answers

You may encounter syntax errors around half-columns.

file A:

var foo = 3 // END OF FILE

file B:

var bar = 4 // BEGINNING OF FILE

file A + B:

var foo = 3var bar = 4

It's easy to solve by simply jamming a comma between each file that you merge.

+3
source

, , . , , , .

+1

As long as you combine them in the correct order, you should be safe. The correct order is the same order that you download it today.

0
source

Source: https://habr.com/ru/post/1702358/


All Articles