• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Resize an iframe to fit its content

Resize an iframe to fit its content

Resize an iframe to fit its content

This snippet gets the height of an iframe’s content and then resizes the height of the iframe to match.

Assume that frame represents the iframe element.

frame.addEventListener('load', function() {
    // Get the height of the content
    const height = frame.contentDocument.body.scrollHeight;

    // Set the height of iframe
    frame.setAttribute('height', `${height}px`);
});

Source

https://htmldom.dev/resize-an-iframe-to-fit-its-content/

Miscellaneous

Related Snippets:

  • Prevent the default action of an event
  • How to Get the Query String in JavaScript
  • Loop over an object’s keys and values
  • How To Remove A Property From A JavaScript Object

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers