最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to correctly include lodashunderscore on website with conflicting libs? - Stack Overflow

programmeradmin2浏览0评论

I have a website that includes a number of third-party js modules via script tag. I need to add lodash or underscore for my code, but if I simply add it from CDN like this:

<script src=".js/2.4.1/lodash.min.js"></script>

then badly written libs die terrible death because they expect _ to be something else. I know that lodash/underscore have something called "no conflict" mode, that requires js code to be executed:

var lodash = _.noConflict();

But this code needs to be executed somewhere, and it's really hard for me to ensure that it's executed before all badly written libs. Is it any simple way to include lodash already in noconflict mode, so i don't need to search for a safe place to enable noconflict mode manually? like lodash.min.noconflict.js?

I have a website that includes a number of third-party js modules via script tag. I need to add lodash or underscore for my code, but if I simply add it from CDN like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>

then badly written libs die terrible death because they expect _ to be something else. I know that lodash/underscore have something called "no conflict" mode, that requires js code to be executed:

var lodash = _.noConflict();

But this code needs to be executed somewhere, and it's really hard for me to ensure that it's executed before all badly written libs. Is it any simple way to include lodash already in noconflict mode, so i don't need to search for a safe place to enable noconflict mode manually? like lodash.min.noconflict.js?

Share Improve this question edited Dec 19, 2014 at 8:15 grigoryvp asked Dec 19, 2014 at 7:08 grigoryvpgrigoryvp 42.5k67 gold badges184 silver badges279 bronze badges 1
  • 1 NO idea. If you find this, please post that as an answer. – Ved Commented Dec 19, 2014 at 11:28
Add a comment  | 

3 Answers 3

Reset to default 12

As long as there are no relevant async scripts before the manual method, it should always work:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
<script>
_u = _.noConflict(); // lets call ourselves _u
</script>

It makes no difference if other scripts set/use _ before or after this. (lodash remembers the last setting of _ and restores it on the _.noConflict() call.)

But if scripts before this are async there is always a possibility that they are allowed to execute between these two scripts. You would have to either use AMD or combine the manual setting into the same script as lodash to avoid races with async scripts.

The latest CDN version is 4.17.15 :)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"> </script>
<script>
_u = _.noConflict();
</script>

You can use the "_" directly in another js file if you have loaded the cdn script tag of lodash.

发布评论

评论列表(0)

  1. 暂无评论