Question

I have been searchin the net but nothing that works as i intend to. What i want is that i have a textfield where the user can pass the URL of a youtube video (in the back-end), then in the front end i want that video to be displayed to use that url. The point is that it wont show the video. When i use the iFrame it works but it needs the url with embaded like this

<iframe width="100%" height="100%" src="http://www.youtube.com/embed/vM89zeKimOY" frameborder="0" allowfullscreen></iframe>

It work fine but as you see the url is not the general url that u copy/paste from the url bar.

the URL of the video is this:

https://www.youtube.com/watch?v=vM89zeKimOY

so what i want is smth like this:

<iframe width="100%" height="100%" src="https://www.youtube.com/watch?v=vM89zeKimOY" frameborder="0" allowfullscreen></iframe>

I dont know if there is a plug-in where you can just paste the url and it plays, or what im missing but a clue or any help would be much appruciated.

Was it helpful?

Solution

using jquery you can simple achieve it.

Html

<input id="TextBoxURL" />
    <br />
    <div id="iframe">        
    </div>

Script

<script type="text/javascript">
        $(document).ready(function () {

            $('#TextBoxURL').change(function () {
                var youTubeUri = $('#TextBoxURL').val();
                var youTubeVideoId = get_youtubeId(youTubeUri);
                loadYouTubeVideo(youTubeVideoId);
            }); 
        });

        function get_youtubeId(url) {
            var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
            var match = url.match(regExp);
            if (match && match[2].length == 11) {
                return match[2];
            } else {
                //error
            }
        }

        function loadYouTubeVideo(id){
            $('#iframe').append("<iframe width='560' height='315' src='//www.youtube.com/embed/"+id+"' frameborder='0' allowfullscreen></iframe>");
        }
    </script>

I didn't include the validation part here with.

Cheers. Happy Coding..!!!

OTHER TIPS

var NormalLink = "https://www.youtube.com/watch?v=vM89zeKimOY";
var theCode = NormalLink.substring(NormalLink.indexOf('?v=')+3);
var embedded = 'https://www.youtube.com/embed/'+theCode;       

FiddleLink: http://jsfiddle.net/swm7v/2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top