Include third party Javascript library which is not included in WordPress

I want to include a third partrty Javascript library, LeafletJS in particular, into my WordPress plugin. I thought it woulnd’t be a good idea just to include it without thinking about what happens if an other plugin includes the same library. Perhaps even in an other (major) version.
I found out that WordPress already has many popular Javascript librarys included. If I want to use one of those I just have to tell WordPress to load these librarys. If every plugin developer does it like this there won’t be any problems and every plugin is using the same version of those librarys.
When I searched the web for how I should include third party librarys I only found many results that described this way. But that won’t help me.

What if I want to include a Javascript library which is not already included in WordPress? Is there any way my plugin won’t have any conflicts with other plugins that include the same library?
Right now I have this problem with a plugin, but it would be nice to have a solution which also works if I have this problem with a theme.

Solutions Collecting From Web of "Include third party Javascript library which is not included in WordPress"

You need to use wp_script_is in order to check if the library you are willing to use has already been included by another plugin. As you can see the handle is important, if you change it to whatEverIWant.js, it will not recognize the library.

There’s no real way that developers would be able to achieve this. This is mostly because:

  • Different plugins may depend on different version of a library, not being able to work with another.
  • Not every developer works clean enough to think of conflicts while creating something. A low quality coded theme/plugin can always cause issue.
  • The same library can be included under different names ( due to carelessness, mistake, etc. ) and if you are publishing a theme/plugin, you won’t always be there for the customer to check for these problems.

However, if you are using a well-known and popular library which is included in WordPress too, you can check if it is loaded already by using the following function:

wp_script_is( $name, $list = 'enqueued' );

This will return true if the script is already printed/enqueued/registered so you can use it with conjunction of an if() to decide whether you should print it again or not.

But as i mentioned above, if the names are not the same, this won’t work.