Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse youtube input links and get youtube ID using jquery ?

I know lots of people asked this sort of question alot but unfortunetly I couldn't sort out my problem .

I have this code in my textarea editor to input a youtube link :

{name:'youtube', key:'Y', replaceWith:'[youtube][![Youtube video Url]!][/youtube]'},

It will ask for youtube video url in a popup window and after that it will put the url between [youtube] and [/youtube].

I would like to have a youtube parser added to it then if member puts youtube url , it will parse the url and get the youtube video ID and input it in the text area like this :

[youtube] ID [/youtube]

Thanks in advance for your help .

like image 266
Jed Ko Avatar asked Dec 11 '25 15:12

Jed Ko


1 Answers

There is a new shortened URL format for youtube URL's... here's an updated script to accomodate both types.

function getVideoId(url){
    if(url.indexOf('?') != -1 ) {
        var query = decodeURI(url).split('?')[1];
        var params = query.split('&');
        for(var i=0,l = params.length;i<l;i++)
            if(params[i].indexOf('v=') === 0)
                return params[i].replace('v=','');
    } else if (url.indexOf('youtu.be') != -1) {
        return decodeURI(url).split('youtu.be/')[1];
    }
    return null;
}
var urlLONG = "http://www.youtube.com/watch?v=UF8uR6Z6KLc&feature=feedlik";
var urlSHORT = "http://youtu.be/UF8uR6Z6KLc";
alert(getVideoId(urlLONG));
alert(getVideoId(urlSHORT));

P.S. I am a flash developer and have only tested this in AS3, but simple stuff like this is identical in both languages. if you want to use it in AS3 just swap the alert() with a trace().

Happy coding!

like image 146
sonicoliver Avatar answered Dec 15 '25 05:12

sonicoliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!