Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Obtener ancho y alto de un iframe con JavaScript

Tengo un iframe en una página. Para este ejemplo, su alto es 0px y el ancho es 100px.

Quiero console.log estos valores con JavaScript.

Por el momento puedo acceder al iframe usando window.frames[0] , pero ¿cómo obtengo el ancho y la altura? Lo que estoy haciendo actualmente devuelve undefined .

Aquí está el código y Fiddle

https://jsfiddle.net/prvmwg5q/

 <!DOCTYPE html> <html lang="en"> <meta charset="UTF-8"> <title>Page Title</title> <meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="stylesheet" href=""> <style> </style> <script src=""></script> <body> <iframe data-search-id="mainline-top" style="height: 0px; width: 100px;"> Test </iframe> <script> console.log(window.frames[0]); console.log(window.frames[0].offsetHeight); // Undefined console.log(window.frames[0].offsetWidth); // Undefined </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

window.frames no devuelve el elemento DOM. https://developer.mozilla.org/en-US/docs/Web/API/Window/frames

En su lugar, asigne una id a su iframe y use getElementById en su lugar.

EDITAR: O, mejor, como señala @HereticMonkey:

document.querySelector('iframe')

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!