Hls.js 使用静态脚本标签,但不使用动态脚本标签
我正在尝试在(打字稿)网页中动态加载 Hls.js 库。 当我在 中使用静态
标记时,它可以正常工作,但是当我尝试在运行时加载 Hls 时,它会失败并显示消息Hls 未定义
所以这有效:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论