Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a youtube video on page using javascript

I'm trying to embed a video on my page via a javascript property :

value: "<iframe title='YouTube video player' type=\"text/html\" width='640' height='390 src='http://www.youtube.com/embed/W-Q7RMpINVo'frameborder='0' allowFullScreen></iframe>"

But when I display this value within the browser the text is displayed instead of the youtube video :

enter image description here

like image 559
blue-sky Avatar asked Oct 29 '25 06:10

blue-sky


2 Answers

You can use two options that are shared here:

  • use document.write:

    var obj = {"video": {
      "value": "<iframe title='YouTube video player' type=\"text/html\" width='640' height='390' src='http://www.youtube.com/embed/W-Q7RMpINVo' frameborder='0' allowFullScreen></iframe>"
    }}
    document.write(obj.video.value);
    

DEMO

  • Use Div and append html using jQuery:

    var obj = {"video": {
    "value": "<iframe title='YouTube video player' type=\"text/html\" width='640'  
    height='390' src='http://www.youtube.com/embed/W-Q7RMpINVo' frameborder='0' 
     allowFullScreen></iframe>"
    }}
    
    $("#test").html(obj.video.value);
    
    
    <div id="test"></div>
    

DEMO

like image 142
Kiran Avatar answered Oct 31 '25 20:10

Kiran


This works for me:

<script>
    document.write("<iframe title='YouTube video player' type=\"text/html\" width='640' height='390' src='http://www.youtube.com/embed/W-Q7RMpINVo'frameborder='0' allowFullScreen></iframe>";
</script>

Btw.: You missed a ' caracter after the height='390.

like image 24
Werner Avatar answered Oct 31 '25 21:10

Werner



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!