how to set iframe height dynamically depending on its content










1















I want to set the iframe height depending on the content height, here below code is working for local links, but i want to do it on dynamically.
please help.



<iframe id="frameDemo" src="https://myvook.blogspot.com"></iframe> 
<script>
$(document).ready(function()
$( "#frameDemo" ).on('load', function()
var mydiv = $(this).contents().find("body");
var h = mydiv.height();
$("#frameDemo").height(h);
);
);











share|improve this question

















  • 1





    If the content of the frame is on a separate domain to the parent window, then you cannot access the content through JS, so what you're attempting to do would not be possible.

    – Rory McCrossan
    Nov 14 '18 at 13:43











  • Thanks Rory, any other ways to make this happen please let me know.

    – Meeravali
    Nov 14 '18 at 14:05















1















I want to set the iframe height depending on the content height, here below code is working for local links, but i want to do it on dynamically.
please help.



<iframe id="frameDemo" src="https://myvook.blogspot.com"></iframe> 
<script>
$(document).ready(function()
$( "#frameDemo" ).on('load', function()
var mydiv = $(this).contents().find("body");
var h = mydiv.height();
$("#frameDemo").height(h);
);
);











share|improve this question

















  • 1





    If the content of the frame is on a separate domain to the parent window, then you cannot access the content through JS, so what you're attempting to do would not be possible.

    – Rory McCrossan
    Nov 14 '18 at 13:43











  • Thanks Rory, any other ways to make this happen please let me know.

    – Meeravali
    Nov 14 '18 at 14:05













1












1








1








I want to set the iframe height depending on the content height, here below code is working for local links, but i want to do it on dynamically.
please help.



<iframe id="frameDemo" src="https://myvook.blogspot.com"></iframe> 
<script>
$(document).ready(function()
$( "#frameDemo" ).on('load', function()
var mydiv = $(this).contents().find("body");
var h = mydiv.height();
$("#frameDemo").height(h);
);
);











share|improve this question














I want to set the iframe height depending on the content height, here below code is working for local links, but i want to do it on dynamically.
please help.



<iframe id="frameDemo" src="https://myvook.blogspot.com"></iframe> 
<script>
$(document).ready(function()
$( "#frameDemo" ).on('load', function()
var mydiv = $(this).contents().find("body");
var h = mydiv.height();
$("#frameDemo").height(h);
);
);








javascript jquery iframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:40









MeeravaliMeeravali

14




14







  • 1





    If the content of the frame is on a separate domain to the parent window, then you cannot access the content through JS, so what you're attempting to do would not be possible.

    – Rory McCrossan
    Nov 14 '18 at 13:43











  • Thanks Rory, any other ways to make this happen please let me know.

    – Meeravali
    Nov 14 '18 at 14:05












  • 1





    If the content of the frame is on a separate domain to the parent window, then you cannot access the content through JS, so what you're attempting to do would not be possible.

    – Rory McCrossan
    Nov 14 '18 at 13:43











  • Thanks Rory, any other ways to make this happen please let me know.

    – Meeravali
    Nov 14 '18 at 14:05







1




1





If the content of the frame is on a separate domain to the parent window, then you cannot access the content through JS, so what you're attempting to do would not be possible.

– Rory McCrossan
Nov 14 '18 at 13:43





If the content of the frame is on a separate domain to the parent window, then you cannot access the content through JS, so what you're attempting to do would not be possible.

– Rory McCrossan
Nov 14 '18 at 13:43













Thanks Rory, any other ways to make this happen please let me know.

– Meeravali
Nov 14 '18 at 14:05





Thanks Rory, any other ways to make this happen please let me know.

– Meeravali
Nov 14 '18 at 14:05












1 Answer
1






active

oldest

votes


















0














Following up with what Rory said, it's not possible to set the iframe height before before the iframe has been loaded since you'll be pulling content from another domain.



However, it is possible to set the height of the iframe after you've downloaded the content. Here is how I've tackled it:



function noteContentIframeLoaded(iframeId) 
// See http://stackoverflow.com/a/5788723 for how to implement this function
var iframeElement = $("#" + iframeId)[0];
setTimeout(function()
setIframeHeight(iframeElement);
, 10);


function setIframeHeight(iframe)
if (iframe) iframe.contentDocument)







share|improve this answer























  • Thanks @Joseph, could you please tell me how to pass a value (postMessage) to iframe from parent page. I don't have access to iframe container page. I want to pass value and get the response in same page itself, do you have any idea. Thanks in advance.

    – Meeravali
    Nov 15 '18 at 14:15












  • What do you mean a value? I included a link that shows how to implement this function.

    – Joseph Cho
    Nov 15 '18 at 17:10











  • Content is coming through URL, so I don't have any iframe document or a page. As that link suggest how to pass the onload function to iframe from main document ?

    – Meeravali
    Nov 16 '18 at 14:00












  • Hi @Joseph, could you please share any jsfiddle example for this. Above mentioned method is not woriking in my case.

    – Meeravali
    Dec 4 '18 at 12:43










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301595%2fhow-to-set-iframe-height-dynamically-depending-on-its-content%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Following up with what Rory said, it's not possible to set the iframe height before before the iframe has been loaded since you'll be pulling content from another domain.



However, it is possible to set the height of the iframe after you've downloaded the content. Here is how I've tackled it:



function noteContentIframeLoaded(iframeId) 
// See http://stackoverflow.com/a/5788723 for how to implement this function
var iframeElement = $("#" + iframeId)[0];
setTimeout(function()
setIframeHeight(iframeElement);
, 10);


function setIframeHeight(iframe)
if (iframe) iframe.contentDocument)







share|improve this answer























  • Thanks @Joseph, could you please tell me how to pass a value (postMessage) to iframe from parent page. I don't have access to iframe container page. I want to pass value and get the response in same page itself, do you have any idea. Thanks in advance.

    – Meeravali
    Nov 15 '18 at 14:15












  • What do you mean a value? I included a link that shows how to implement this function.

    – Joseph Cho
    Nov 15 '18 at 17:10











  • Content is coming through URL, so I don't have any iframe document or a page. As that link suggest how to pass the onload function to iframe from main document ?

    – Meeravali
    Nov 16 '18 at 14:00












  • Hi @Joseph, could you please share any jsfiddle example for this. Above mentioned method is not woriking in my case.

    – Meeravali
    Dec 4 '18 at 12:43















0














Following up with what Rory said, it's not possible to set the iframe height before before the iframe has been loaded since you'll be pulling content from another domain.



However, it is possible to set the height of the iframe after you've downloaded the content. Here is how I've tackled it:



function noteContentIframeLoaded(iframeId) 
// See http://stackoverflow.com/a/5788723 for how to implement this function
var iframeElement = $("#" + iframeId)[0];
setTimeout(function()
setIframeHeight(iframeElement);
, 10);


function setIframeHeight(iframe)
if (iframe) iframe.contentDocument)







share|improve this answer























  • Thanks @Joseph, could you please tell me how to pass a value (postMessage) to iframe from parent page. I don't have access to iframe container page. I want to pass value and get the response in same page itself, do you have any idea. Thanks in advance.

    – Meeravali
    Nov 15 '18 at 14:15












  • What do you mean a value? I included a link that shows how to implement this function.

    – Joseph Cho
    Nov 15 '18 at 17:10











  • Content is coming through URL, so I don't have any iframe document or a page. As that link suggest how to pass the onload function to iframe from main document ?

    – Meeravali
    Nov 16 '18 at 14:00












  • Hi @Joseph, could you please share any jsfiddle example for this. Above mentioned method is not woriking in my case.

    – Meeravali
    Dec 4 '18 at 12:43













0












0








0







Following up with what Rory said, it's not possible to set the iframe height before before the iframe has been loaded since you'll be pulling content from another domain.



However, it is possible to set the height of the iframe after you've downloaded the content. Here is how I've tackled it:



function noteContentIframeLoaded(iframeId) 
// See http://stackoverflow.com/a/5788723 for how to implement this function
var iframeElement = $("#" + iframeId)[0];
setTimeout(function()
setIframeHeight(iframeElement);
, 10);


function setIframeHeight(iframe)
if (iframe) iframe.contentDocument)







share|improve this answer













Following up with what Rory said, it's not possible to set the iframe height before before the iframe has been loaded since you'll be pulling content from another domain.



However, it is possible to set the height of the iframe after you've downloaded the content. Here is how I've tackled it:



function noteContentIframeLoaded(iframeId) 
// See http://stackoverflow.com/a/5788723 for how to implement this function
var iframeElement = $("#" + iframeId)[0];
setTimeout(function()
setIframeHeight(iframeElement);
, 10);


function setIframeHeight(iframe)
if (iframe) iframe.contentDocument)








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 14:08









Joseph ChoJoseph Cho

1,96321023




1,96321023












  • Thanks @Joseph, could you please tell me how to pass a value (postMessage) to iframe from parent page. I don't have access to iframe container page. I want to pass value and get the response in same page itself, do you have any idea. Thanks in advance.

    – Meeravali
    Nov 15 '18 at 14:15












  • What do you mean a value? I included a link that shows how to implement this function.

    – Joseph Cho
    Nov 15 '18 at 17:10











  • Content is coming through URL, so I don't have any iframe document or a page. As that link suggest how to pass the onload function to iframe from main document ?

    – Meeravali
    Nov 16 '18 at 14:00












  • Hi @Joseph, could you please share any jsfiddle example for this. Above mentioned method is not woriking in my case.

    – Meeravali
    Dec 4 '18 at 12:43

















  • Thanks @Joseph, could you please tell me how to pass a value (postMessage) to iframe from parent page. I don't have access to iframe container page. I want to pass value and get the response in same page itself, do you have any idea. Thanks in advance.

    – Meeravali
    Nov 15 '18 at 14:15












  • What do you mean a value? I included a link that shows how to implement this function.

    – Joseph Cho
    Nov 15 '18 at 17:10











  • Content is coming through URL, so I don't have any iframe document or a page. As that link suggest how to pass the onload function to iframe from main document ?

    – Meeravali
    Nov 16 '18 at 14:00












  • Hi @Joseph, could you please share any jsfiddle example for this. Above mentioned method is not woriking in my case.

    – Meeravali
    Dec 4 '18 at 12:43
















Thanks @Joseph, could you please tell me how to pass a value (postMessage) to iframe from parent page. I don't have access to iframe container page. I want to pass value and get the response in same page itself, do you have any idea. Thanks in advance.

– Meeravali
Nov 15 '18 at 14:15






Thanks @Joseph, could you please tell me how to pass a value (postMessage) to iframe from parent page. I don't have access to iframe container page. I want to pass value and get the response in same page itself, do you have any idea. Thanks in advance.

– Meeravali
Nov 15 '18 at 14:15














What do you mean a value? I included a link that shows how to implement this function.

– Joseph Cho
Nov 15 '18 at 17:10





What do you mean a value? I included a link that shows how to implement this function.

– Joseph Cho
Nov 15 '18 at 17:10













Content is coming through URL, so I don't have any iframe document or a page. As that link suggest how to pass the onload function to iframe from main document ?

– Meeravali
Nov 16 '18 at 14:00






Content is coming through URL, so I don't have any iframe document or a page. As that link suggest how to pass the onload function to iframe from main document ?

– Meeravali
Nov 16 '18 at 14:00














Hi @Joseph, could you please share any jsfiddle example for this. Above mentioned method is not woriking in my case.

– Meeravali
Dec 4 '18 at 12:43





Hi @Joseph, could you please share any jsfiddle example for this. Above mentioned method is not woriking in my case.

– Meeravali
Dec 4 '18 at 12:43



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301595%2fhow-to-set-iframe-height-dynamically-depending-on-its-content%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto