Both main window and iframe are on the same domain, but what I want to achieve is to resize iframe to fit its contents using js only within that iframe.
I also do not know the id of the iframe that is assigned to it by main window. And I do not want to use jquery or any other framework for it.
You can do this also without knowing the
id
of theiframe
in the parent window:See
frameElement
at MDN.EDIT
Just in case the
iframe
happens to be in a container element which has fixed size andoverflow: hidden
you can do something like this:Assuming you have only 1 IFRAME in the main window, from the Javascript of the IFRAME's document you can do something like:
For same-origin content like yours, yes you can.
To borrow from Gary's answer on the above question:
Try accessing the iframe using parent.document, and see if this solves your issue.