简体   繁体   中英

How to use a Bowser Javascript Library in index.html in script tags?

I need to use a Javascript Library in my AngularJS app, but i need this already in index.html before the angular app is initialized.

This is the library: https://github.com/lancedikson/bowser

And it is normally used like this:

const browser = Bowser.getParser(window.navigator.userAgent);

console.log(`The current browser name is "${browser.getBrowserName()}"`);
// The current browser name is "Internet Explorer"

The problem is, after i define the library in script tags:

<script src="lib/bower/..../bowser.js"></script>

Can i immediately use this in the next script tag within the index.html like this?

<script>
    const browser = Bowser.getParser(window.navigator.userAgent);
    ...
</script>

At the moment, Bowser is undefined.

After checking the Bowser code it looks like it is not capitalised. Further the bowser library file you are serving may not be intended to be used that way. One solution is to serve a built version of bowser from a CDN:

<script src="https://cdn.jsdelivr.net/npm/bowser@2.5.3/es5.min.js"></script>
<script>
    const browser = bowser.getParser(window.navigator.userAgent);
    //...
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM