Embedding Media

Embedding is the process of including media from other sources into your web sites.

"other documents" can include:

"embedding" is sometimes also called "including" or "transcluding"

Interactive Embedding

Sometimes you can use JavaScript to interact with the embedded file or app as if it were another User Interface element on your page!

For instance, you could programmatically pause and play an embedded audio clip, say, playing "hoorah!" when the user wins a game of Tic-Tac-Toe.

Or, if the user moves their location on an embedded Google Map, your app could be notified and find the city name and look up restaurants in that area on Yelp's API.

This lesson focuses on static embedding: displaying media and allowing the user to interact with it directly. The Interactive Embedding lesson focuses on interactive embedding: using JavaScript to pass messages between your page's scripts and the embedded media.

Embedding Images

<img src=''> is the original embed

MDN: img

Where does the media come from?

Embedding IFrames

<iframe> means "inline frame"


<iframe height='300' width='400' src='example.com'></iframe>

Malware Vectors

Beware the "same origin policy":

Refused to display 'http://www.burlingtoncodeacademy.com/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

Beware giving foreign JavaScript access to your page contents and user-input data

Embedding HTML snippets

You can use iframes to load the same HTML onto several pages on your site.

This is often used for login boxes.

Warning: iframes can take longer to load than the rest of the page, and can eat up CPU and RAM, so don't overuse them

Embedding Flash

Adobe Flash
1996 - 2015

"In July 2017, Adobe announced that it would declare Flash to be end-of-life in 2020, and will cease support, distribution, and security updates to Flash Player."

Embedding Video

HTML5 defines a standard <video> tag

Tip: you can use automatically start playing the video when the page loads; to be polite you should also mute the volume, like this: <video muted=true autoplay=true src='/videos/yelling-man.mp4'>

Embedding Audio


Playing Audio from JavaScript

Playing sounds directly from JavaScript is pretty easy:

let audio = new Audio('audio_file.mp3');

Embedding Maps

<iframe id="inlineFrameExample"
    title="Inline Frame Example"

OpenStreetMaps defines a "bounding box" as a four-tuple: min Longitude, min Latitude, max Longitude, max Latitude.

You can find the bounding box for a given map on https://www.openstreetmap.org/ by clicking the Export button.

Scripting Maps

It is possible to use JavaScript to interact with an embedded map. See the client-side coding lesson on embedding

 Previous Lesson