首页 > 解决方案 > 如何在脚本标签的 index.html 中使用 Bowser Javascript 库?

问题描述

我需要在我的 AngularJS 应用程序中使用 Javascript 库,但在初始化 Angular 应用程序之前,我需要在 index.html 中使用它。

这是图书馆:https ://github.com/lancedikson/bowser

它通常是这样使用的:

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

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

问题是,在我在脚本标签中定义库之后:

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

我可以像这样在 index.html 中的下一个脚本标签中立即使用它吗?

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

目前,Bowser 是未定义的。

标签: javascriptangularjs

解决方案


检查Bowser代码后,它看起来没有大写。此外,您提供的 bowser 库文件可能不打算以这种方式使用。一种解决方案是从 CDN 提供构建版本的 bowser:

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

推荐阅读