0

I want to write a iframe to html from javascript the below code dosent work can anyone explain why

 <html>
<body>

<script>
document.write("<iframe width="560" height="315" src="https://www.youtube.com/embed/9Ow8QgoXzeE" frameborder="0" allowfullscreen></iframe>");
</script>

</body>
</html> 
avilac
  • 792
  • 1
  • 8
  • 23
salih
  • 324
  • 1
  • 7
  • 14

2 Answers2

1

Use sublime to ensure you don't do such mistakes. It's mistake of " and ' :

either way it will work:

<script>
document.write('<iframe width="560" height="315" src="https://www.youtube.com/embed/9Ow8QgoXzeE" frameborder="0" allowfullscreen></iframe>');
</script>

or

<script>
document.write("<iframe width='560' height='315' src='https://www.youtube.com/embed/9Ow8QgoXzeE' frameborder='0' allowfullscreen></iframe>");
</script>

In your code, reason why it doesn't work is that you are using all way : " and that is why it will not be recognizable where your string ends and starts.

For more clarification, see this:

var ex = "its "important" to know";//wrong
var ex = "its 'important' to know";//right
var ex = 'its "important" to know';//right

You can see that first statement gets processed as one string: "its " and other string "to know" but not characters between(important).

Abhay Maurya
  • 11,819
  • 8
  • 46
  • 64
0

I managed to make it work, find below the new code:

Javascript:

document.write("<iframe width='560' height='315' src='https://www.youtube.com/embed/9Ow8QgoXzeE' frameborder='0' allowfullscreen></iframe>");

And here the working example:

https://jsfiddle.net/fzzkb1jx/

Hope it helps!

avilac
  • 792
  • 1
  • 8
  • 23