Hls.js 使用静态脚本标签,但不使用动态脚本标签

发布于 2025-01-11 10:23:32 字数 820 浏览 3 评论 0原文

我正在尝试在(打字稿)网页中动态加载 Hls.js 库。 当我在 中使用静态

所以这有效:

<script src="https://cdn.jsdelivr.net/npm/hls.js"></script>
<script>
console.log(Hls.isSupported());
</script>

但这不起作用(jQuery):

$.getScript("https://cdn.jsdelivr.net/npm/hls.js", (script, status, xhr) => { 
    console.log(Hls.isSupported());
}); 

更传统的方法也不起作用:

const script = document.createElement("script");
script.setAttribute("src", "https://cdn.jsdelivr.net/npm/hls.js");
script.onload = () => {
    console.log(Hls.isSupported());
}
document.body.appendChild(script);

在这两种情况下,我都可以看到正在下载的脚本并显示在浏览器的调试器中窗格 (F12),但是Hls 未定义。

I'm trying to dynamically load the Hls.js library in a (typescript) webpage.
When I use a static <script> tag inside <head>, it works without a problem, but when I try to load Hls at runtime it fails with the message Hls is not defined

So this works:

<script src="https://cdn.jsdelivr.net/npm/hls.js"></script>
<script>
console.log(Hls.isSupported());
</script>

But this doesn't (jQuery):

$.getScript("https://cdn.jsdelivr.net/npm/hls.js", (script, status, xhr) => { 
    console.log(Hls.isSupported());
}); 

And neither does a more traditional approach:

const script = document.createElement("script");
script.setAttribute("src", "https://cdn.jsdelivr.net/npm/hls.js");
script.onload = () => {
    console.log(Hls.isSupported());
}
document.body.appendChild(script);

In both cases, I can see the script being downloaded and showing up in the browser's Debugger pane (F12), but Hls is not defined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文