I'm building a widget which would be used in different eCommerce sites.
As i'm using underscorejs library, i need to see if underscorejs exists in the parent site and if so what version.
If underscore is already defined in the site and if the version matches with the one i used. I would inturn use their own defined underscore and not make a CDN call to load underscorejs for my widget.
I'm building a widget which would be used in different eCommerce sites.
As i'm using underscorejs library, i need to see if underscorejs exists in the parent site and if so what version.
If underscore is already defined in the site and if the version matches with the one i used. I would inturn use their own defined underscore and not make a CDN call to load underscorejs for my widget.
Share Improve this question asked Nov 5, 2014 at 13:53 Ashwath GovindAshwath Govind 531 silver badge4 bronze badges 03 Answers
Reset to default 6You can easily check that with the following
if (!window._ || window._.VERSION !== '1.7.0') {
// Load underscore from CDN
}
Get the version of underscorejs used
Check the library itself. Most likely it retained the copyright ment on top of the file.
i need to see if underscorejs exists in the parent site
_.VERSION
You can use this pattern to load library from HTML:
<script>
(window._ && window._.VERSION === '1.7.0') || document.write("<script src='//cdnjs.cloudflare./ajax/libs/underscore.js/1.7.0/underscore-min.js'>\x3C/script>");
</script>
You can put this script at the beginning to the widget HTML.