【问题标题】:how to use embed code in javascript variable如何在javascript变量中使用嵌入代码
【发布时间】:2021-06-02 22:53:28
【问题描述】:

我在 js 中有这个变量:

var video_embed = '<script src="https://stream.laminor.academy/1qwyvs9ystd9/embed"><\/script>';

$('.my_video').after("<div class='col-xs-12' id='video_content'>"
 + video_embed + 
"</div>");

但我呈现的 HTML 代码是:

<div class="col-xs-12" id="video_content">
    <script src="https://stream.laminor.academy/1qwyvs9ystd9/embed"></script>
</div>

嵌入代码不起作用!

感谢您的帮助

【问题讨论】:

  • “但我渲染的 HTML 代码是”——你希望它是什么?
  • @Quentin,从我的视频服务渲染视频标签
  • 为什么会发生这种情况(请记住,您在示例中使用的 URL 是 404 错误)?
  • 此网址为示例。在 HTML 中使用 &lt;script src="https://example.com/VIDEO_ID/embed"&gt;&lt;/script&gt; 时,这是可行的。但我想附加 js
  • "this url is example" — 显然,但这并没有给我们任何继续调试的机会。您需要提供minimal reproducible example

标签: javascript html jquery embed


【解决方案1】:

当你在文档加载后生成脚本标签时,它不会运行。

你需要下载脚本并执行它:


//Code that will download the script
function loadScript() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
       // Making sure there's no error
        if (this.readyState == 4 && this.status == 200) {
            // Executing the script
            eval(this.responseText)
        } else if (this.readyState == 4) {
            // If the server responded with something else than a 200 OK
            console.warn(this.status)
        }
    };
    xhttp.open("GET", "https://stream.laminor.academy/1qwyvs9ystd9/embed", true);
    xhttp.send();
}
// Executing the function
loadScript()

如果它不起作用,请告诉我。

【讨论】:

  • 谢谢。如何将此函数附加到 HTML 代码中?
  • @MostafaNorzade 我不确定,但也许如果你把上面的代码放在你的 div 的
猜你喜欢
  • 2023-03-27
  • 2014-06-03
  • 2014-05-02
  • 2011-03-22
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
相关资源
最近更新 更多