【问题标题】:JQuery explode prompt result not working when loading a URL加载URL时JQuery爆炸提示结果不起作用
【发布时间】:2015-10-05 09:37:01
【问题描述】:

我正在尝试创建一个脚本,要求提供 YouTube 视频链接,然后将其分解并自动将其插入到编辑器中。

但是这个技巧不起作用。

function TinyMCEInsertYouTube() {
    var YouTubeLink     = encodeURIComponent(prompt("Please insert the YouTube link"));
    var result          = $(YouTubeLink).text().split('watch?v=');
    var VideoIDParam    = result[1];

    var VideoHeight     = prompt("Please enter the video height");
    var VideoWidth      = prompt("Please enter the video width");

    var InsertCode = '<iframe width="' + VideoWidth + '" height="' + VideoHeight + '" src="https://www.youtube.com/embed/' + VideoIDParam + '" frameborder="0" allowfullscreen></iframe>'

    tinyMCE.activeEditor.insertContent( InsertCode );
}

它会产生以下错误:

错误:语法错误,无法识别的表达式:https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrPpO65UbM6Y

我已经尝试添加以下标签: encodeURIComponent()

但没有结果

我尝试添加 escape() 而不是 encodeURIComponent 也没有结果。

出现以下错误:

错误:语法错误,无法识别的表达式:https%3A//www.youtube.com/watch%3Fv%3DrPpO65UbM6Y

编辑!

有效!!感谢 @SearchAndResQ

以下代码:

function TinyMCEInsertYouTube() {
    var YouTubeLink     = prompt("Please insert the YouTube link");
    var result          = YouTubeLink.split('watch?v=');
    var VideoIDParam    = result[1];

    var VideoHeight     = prompt("Please enter the video height");
    var VideoWidth      = prompt("Please enter the video width");

    var InsertCode = '<iframe width="' + VideoWidth + '" height="' + VideoHeight + '" src="https://www.youtube.com/embed/' + VideoIDParam + '" frameborder="0" allowfullscreen></iframe>'

    tinyMCE.activeEditor.insertContent( InsertCode );
}

【问题讨论】:

    标签: jquery url


    【解决方案1】:

    YouTubeLink 是一个包含输入文本的变量。可以直接拆分:

    var result = YouTubeLink.split('watch?v=');

    而且你不需要encodeURIComponent

    【讨论】:

    • 另一个问题是链接是URI编码的。因此,split() 实际上不会拆分任何内容,因为? 被编码为%3F
    • 但是escape()? 做的事情完全一样
    • 你又是对的(很明显,我的 JavaScript 不太好)。想知道它对他是如何工作的,也许他不再使用逃生了。如果您觉得这对任何人没有帮助,您可以投反对票。
    • 链接保存在提示符下的变量 YouTubeLink 中。
    【解决方案2】:

    您可以直接拆分Youtube链接,因为YoutubeLink是包含输入文本的变量。

    也不需要encodeURIComponent

    检查我为你做的这个例子:

    JSFIDDLE EXAMPLE

    【讨论】:

      猜你喜欢
      • 2014-05-05
      • 2021-09-10
      • 1970-01-01
      • 2019-04-24
      • 2013-02-13
      • 2012-03-30
      • 1970-01-01
      • 2011-12-25
      • 2012-08-30
      相关资源
      最近更新 更多