1

I am trying to create a script that asks for a YouTube video link and following explodes it and automatically inserts it into the editor.

The trick is however not working.

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 );
}

It generates the following error :

Error: Syntax error, unrecognized expression: https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DrPpO65UbM6Y

I already tried to add the following tags : encodeURIComponent()

But to no result

I tried to add escape() instead of encodeURIComponent Also no result.

Gave the following error :

Error: Syntax error, unrecognized expression: https%3A//www.youtube.com/watch%3Fv%3DrPpO65UbM6Y

Edit!

It works!! Thanks to @SearchAndResQ

The following code :

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 );
}

2 Answers 2

2

YouTubeLink is a variable containing the inputted text. you can split it directly:

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

And you don't need to encodeURIComponent

Sign up to request clarification or add additional context in comments.

4 Comments

Another problem is that the link is URI Encoded. As such, the split() will not actually split anything as the ? is encoded as %3F.
But escape() does exactly the same thing to ?
You are right again (I am not that good in javascript, obviously). Wonder how it works for him, maybe he is not using escape anymore. You can down vote this if you feel it is not helping anyone.
The link is saved in the variable YouTubeLink from the prompt.
1

You can directly split the Youtube link because YoutubeLink is the variable containing the inputted text.

Also there is no need for encodeURIComponent

Check this example i've done for you:

JSFIDDLE EXAMPLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.