HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML srcdoc Attribute
Photo Credit to CodeToFun
🙋 Introduction
The srcdoc
attribute is a useful feature in HTML, primarily used with the <iframe> element.
It allows developers to embed HTML content directly within the srcdoc
attribute, eliminating the need for a separate external file specified by the src attribute.
This attribute provides a convenient way to include inline content within an iframe, making it a versatile tool for web development.
🎯 Purpose of srcdoc
The primary purpose of the srcdoc
attribute is to specify the HTML content that should be displayed within an <iframe> element.
Unlike the traditional approach of using the src attribute to link to an external document, srcdoc allows developers to include the content directly in the HTML document.
💎 Values
The srcdoc
attribute accepts a string value containing the HTML content to be displayed within the iframe.
This value can include any valid HTML markup, including text, images, and other elements.
It provides a convenient way to create self-contained iframes without the need for external files.
📄 Example
Let's look at a simple example of how to use the srcdoc
attribute in an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML srcdoc Attribute</title>
</head>
<body>
<h1>Embedding Content with srcdoc</h1>
<iframe srcdoc="<p>This is the embedded content within the iframe.</p>" width="300" height="200"></iframe>
</body>
</html>
🧠 How it Works
In this example, the srcdoc
attribute is used to directly embed a simple paragraph within the <iframe> element. This can be particularly useful when you want to include lightweight content without creating a separate HTML file.
🔄 Dynamic Values with JavaScript
You can also dynamically set the srcdoc
attribute using JavaScript.
This can be useful when you want to update the content of the iframe based on user interactions or other dynamic events. Here's a brief example:
<script>
// Dynamically set srcdoc for an iframe
const dynamicContent = "<p>This content is dynamically set with JavaScript.</p>";
document.getElementById("dynamicIframe").srcdoc = dynamicContent;
</script>
🧠 How it Works
In this script, the srcdoc
attribute for an iframe with the id "dynamicIframe" is dynamically set to a new HTML content string. This flexibility allows developers to update iframe content on the fly.
🏆 Best Practices
- Use the
srcdoc
attribute when you want to embed simple and self-contained HTML content within an iframe. - Be mindful of security considerations, as dynamically setting content with JavaScript may expose your application to potential vulnerabilities.
- Test your iframes across different browsers to ensure consistent behavior, as browser support for certain features may vary.
🎉 Conclusion
The srcdoc
attribute provides a convenient way to embed HTML content within iframes directly.
Whether you're including static content or dynamically updating it with JavaScript, understanding how to use srcdoc enhances your ability to create interactive and engaging web pages.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML srcdoc Attribute), please comment here. I will help you immediately.