Alternatives to iframe srcdoc?

后端 未结 3 1491
无人共我
无人共我 2020-12-14 03:29

Generally I am against the use of iframes, but it solved a particular problem of mine.

The thing is that I have a tinyMCE-editor at a webpage. After the user have ma

3条回答
  •  暖寄归人
    2020-12-14 03:41

    As suggested by eicto by comment, jquery could be used to fill an iframe at the ready-event. In order to adjust the height of the iframe to the height of the content some dirty hacks had to be applied, but the code I ended up using is more or less:

    HTML

    
    
    
    

    JS

    // Wait until iFrame is ready (body is then available)
    $('#myIframe').ready(function() {
    
        var externalHtml = '

    Hello World!

    '; // Find the body of the iframe and set its HTML // Add a wrapping div for height hack // Set min-width on wrapping div in order to get real height afterwords $('#myIframe').contents().find('body') .html('
    ' +externalHtml +'
    ' ); // Let the CSS load before getting height setTimeout(function() { // Set the height of the iframe to the height of the content $('#myIframe').css('height', $('#myIframe').contents() .find('#iframeContent').height() + 'px' ); },50); });

提交回复
热议问题