Override body style for content in an iframe

前端 未结 10 944
青春惊慌失措
青春惊慌失措 2020-11-22 01:10

How can I control the background image and colour of a body element within an iframe? Note, the embedded body element has a class, and the iframe i

相关标签:
10条回答
  • 2020-11-22 01:16

    An iframe is a 'hole' in your page that displays another web page inside of it. The contents of the iframe is not in any shape or form part of your parent page.

    As others have stated, your options are:

    • give the file that is being loaded in the iframe the necessary CSS
    • if the file in the iframe is from the same domain as your parent, then you can access the DOM of the document in the iframe from the parent.
    0 讨论(0)
  • 2020-11-22 01:16

    Override another domain iframe CSS

    By using part of SimpleSam5's answer, I achieved this with a few of Tawk's chat iframes (their customization interface is fine but I needed further customizations).

    In this particular iframe that shows up on mobile devices, I needed to hide the default icon and place one of my background images. I did the following:

    Tawk_API.onLoad = function() {
    // without a specific API, you may try a similar load function
    // perhaps with a setTimeout to ensure the iframe's content is fully loaded
      $('#mtawkchat-minified-iframe-element').
        contents().find("head").append(
         $("<style type='text/css'>"+
           "#tawkchat-status-text-container {"+
             "background: url(https://example.net/img/my_mobile_bg.png) no-repeat center center blue;"+
             "background-size: 100%;"+
           "} "+
           "#tawkchat-status-icon {display:none} </style>")
       );
    };
    

    I do not own any Tawk's domain and this worked for me, thus you may do this even if it's not from the same parent domain (despite Jeremy Becker's comment on Sam's answer).

    0 讨论(0)
  • 2020-11-22 01:23

    I have a blog and I had a lot of trouble finding out how to resize my embedded gist. Post manager only allows you to write text, place images and embed HTML code. Blog layout is responsive itself. It's built with Wix. However, embedded HTML is not. I read a lot about how it's impossible to resize components inside body of generated iFrames. So, here is my suggestion:

    If you only have one component inside your iFrame, i.e. your gist, you can resize only the gist. Forget about the iFrame.

    I had problems with viewport, specific layouts to different user agents and this is what solved my problem:

    <script language="javascript" type="text/javascript" src="https://gist.github.com/roliveiravictor/447f994a82238247f83919e75e391c6f.js"></script>
    
    <script language="javascript" type="text/javascript">
    
    function windowSize() {
      let gist = document.querySelector('#gist92442763');
    
      let isMobile = {
        Android: function() {
            return /Android/i.test(navigator.userAgent)
        },
        BlackBerry: function() {
            return /BlackBerry/i.test(navigator.userAgent)
        },
        iOS: function() {
            return /iPhone|iPod/i.test(navigator.userAgent)
        },
        Opera: function() {
            return /Opera Mini/i.test(navigator.userAgent)
        },
        Windows: function() {
            return /IEMobile/i.test(navigator.userAgent) || /WPDesktop/i.test(navigator.userAgent)
        },
        any: function() {
            return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
        }
    };
    
      if(isMobile.any()) {
        gist.style.width = "36%";
        gist.style.WebkitOverflowScrolling = "touch"
        gist.style.position = "absolute"
      } else {
        gist.style.width = "auto !important";
      }
    }
    
    windowSize();
    
    window.addEventListener('onresize', function() {
        windowSize();
    });
    
    </script>
    
    <style type="text/css">
    
    .gist-data {
      max-height: 300px;
    }
    
    .gist-meta {
      display: none;
    }
    
    </style>
    

    The logic is to set gist (or your component) css based on user agent. Make sure to identify your component first, before applying to query selector. Feel free to take a look how responsiveness is working.

    0 讨论(0)
  • 2020-11-22 01:29

    This code uses vanilla JavaScript. It creates a new <style> element. It sets the text content of that element to be a string containing the new CSS. And it appends that element directly to the iframe document's head.

    Keep in mind, however, that accessing elements of a document loaded from another origin is not permitted (for security reasons) -- contentDocument of the iframe element will evaluate to null when attempted from the browsing context of the page embedding the frame.

    var iframe = document.getElementById('the-iframe');
    var style = document.createElement('style');
    style.textContent =
      'body {' +
      '  background-color: some-color;' +
      '  background-image: some-image;' +
      '}' 
    ;
    iframe.contentDocument.head.appendChild(style);
    
    0 讨论(0)
  • 2020-11-22 01:30

    You cannot change the style of a page displayed in an iframe unless you have direct access and therefore ownership of the source html and/or css files.

    This is to stop XSS (Cross Site Scripting)

    0 讨论(0)
  • 2020-11-22 01:34

    give the body of your iframe page an ID (or class if you wish)

    <html>
    <head></head>
    <body id="myId">
    </body>
    </html>
    

    then, also within the iframe's page, assign a background to that in CSS

    #myId {
        background-color: white;
    }
    
    0 讨论(0)
提交回复
热议问题