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> 
2
  • First, you need \" inside the string. Write document.write("<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/9Ow8QgoXzeE\" frameborder=\"0\" allowfullscreen></iframe>"); Commented Jan 5, 2017 at 14:50
  • 4
    This has already been answered. stackoverflow.com/questions/8726455/… Commented Jan 5, 2017 at 14:51

2 Answers 2

2

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).

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

Comments

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.