<iframe> The Inline Frame
title="Has To Have A Title"
src="url of the link you want to embed">
<iframe> helps with embedding any youtube videos , maps or more , even a google calendar to your project, yes you read it right, you can embed google Calendar to your web app using
For my project i worked with a random meal generating API that also happened to have URL for youtube videos.
I felt like, what a perfect way to look at a recipe by watching a Youtube video on how to make a meal. So i decided to use
In next steps, i will show how i embedded random youtube videos to my project using
If you are in a situation like me where you are using user input to change the
src of <iframe/> and you want a different URL for your source every time your page renders, then you may come across an issue with youtube videos urls, Youtube URLs can have watch in them as shown below but in order for the link to work with
<iframe/> it has to have embed instead of watch. If it is a single URL you can just change that yourself
Example shown below, shows steps on how i fixed that issue in my project, i mapped over my array of recipes and got my youtube url then worked on following steps to break my url and then replace watch with embed
get the Urlstep 2:
const url = showRecipe.map(x => x.strYoutube.split("/"))
const cutFirst = url.flat().splice(0, 3)
const first = cutFirst.join("/")
const cutLast = url.flat().splice(3, 4).toString()
const last = cutLast.slice(8, 19)
const newUrl = first + "/" + "embed" + "/" + last
and results in console for each step
if there is any other way around it that’s fine too, this is what i had to come up with to use my random meal generating api videos
after i was able to get my newURL this is how i embedded it using
This way every time my page refreshed my new random video generated with that newUrl
<iframe/> element provides as a great resource and really helpful when you want to use these already existing sites to make your website more powerful.
i also added a Google calendar to my site using the already existing
<iframe> code that exists in the settings of Google calendar
Iframe is one of the oldest HTML elements, and its resilience and usefulness tell us it’s here to stay for a while. For embedding content, this element is a must for your HTML tool belt.