How can an iframe get its parent background color?

Dylan Valade

I have a Pardot (Salesforce) form displayed in an iframe and control over the markup and scripts on both domains. The form has a transparent background and sometimes the parent page's background photo or color does not have enough contrast with the text color of form labels. You can't read First Name when it's dark text on a dark background.

My goal is to set a body class inside the iframe that is .light-bg or .dark-bg based on a color sampling of the parent element containing the frame.

In this code the iframe would be able to determine if div.framed-lead-form has a light or dark background. There are JS plugins to get an element's color saturation (this one has an interesting license https://gist.github.com/larryfox/1636338) but I can't find anything that works through iframes.

<div class="framed-lead-form">
    <iframe src="//go.pardot.com/id" allowtransparency="true"></iframe>
</div>
Kristapsv

Made a fiddle with main document and iframe.
From main document sends theme with window post message
From iframe listens to message and sets background theme color

Main document: https://jsfiddle.net/kristapsv/L1fd64b3/33/

(function($){

$.fn.lightOrDark = function(){
var r,b,g,hsp
  , a = this.css('background-color');

if (a.match(/^rgb/)) {
  a = a.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/);
  r = a[1];
  b = a[2];
  g = a[3];
} else {
  a = +("0x" + a.slice(1).replace( // thanks to jed : http://gist.github.com/983661
      a.length < 5 && /./g, '$&$&'
    )
  );
  r = a >> 16;
  b = a >> 8 & 255;
  g = a & 255;
}
hsp = Math.sqrt( // HSP equation from http://alienryderflex.com/hsp.html
  0.299 * (r * r) +
  0.587 * (g * g) +
  0.114 * (b * b)
);
if (hsp>127.5) {
  return 'light';
} else {
  return 'dark';
}
}

})(jQuery);

var iframe = document.getElementById('my-iframe');

// Set here light/dark depending on container or whatever color
var theme =  $('.container').lightOrDark();

var jsonMessage = {
  'action' : 'set-theme',
  'theme' : theme
};

iframe.contentWindow.postMessage(JSON.stringify(jsonMessage), '*');

Iframe: https://jsfiddle.net/kristapsv/zLL9db1c/11/

var messageHandler = function (event) {
  var message;

  try {
    message = JSON.parse(event.data);
  } catch (e) {
    return;
  }

  switch (message.action) {
    case 'set-theme':
        setTheme(message.theme);
        break;
  }
};

var setTheme = function(theme) {
  $('.text-container')
    .removeClass('dark')
    .removeClass('light')
    .addClass(theme);
}

window.addEventListener('message', messageHandler, false);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JQuery Get Parent URL in IFrame:iframe and its parent are in Different domain

From Dev

How to just scroll the iframe but not its parent?

From Dev

how to change background color of Qwidget irrespective of its parents background color

From Dev

how can I get the menubar background color fit to the page?

From Dev

How can I get the background color of an HTML table's row?

From Dev

How to fill parent element background color with CSS?

From Dev

How can I make a button change its background color when pressed down?

From Dev

How can i change select box arrow when its have background color

From Dev

how to get the background color in vim?

From Dev

How to get an iframe to hold its position?

From Dev

Why when I create this new Bitmap its background is dark grey? How can I set it to the same color of the layout background?

From Dev

How to get the parent window URL from Iframe?

From Dev

How to get the parent window URL from Iframe?

From Dev

How to make width and height of iframe same as its parent div?

From Dev

How to copy a cloned node in an iframe to its parent with styles?

From Dev

Can't get the background color using DOM

From Dev

Child div needs to overlap on its parent such that it gets parent's background color

From Dev

Selector to get parent of <span> text to change its color

From Dev

Selector to get parent of <span> text to change its color

From Dev

ReactJS - How can a child find its parent?

From Dev

ReactJS - How can a child find its parent?

From Dev

How to run a program and get its PID in the background

From Dev

Given the following HTML and CSS how can I get the input elment to fill the width of its parent?

From Dev

How to make a div mask its parent's background

From Dev

uwp: how to change background color of listview item based on its value?

From Dev

How to change the background color of a tkinter Canvas after its' creation?

From Dev

How can I change background color for the desktop?

From Dev

How can I set Background Color for a Scene?

From Dev

How can I change background color for the desktop?

Related Related

  1. 1

    JQuery Get Parent URL in IFrame:iframe and its parent are in Different domain

  2. 2

    How to just scroll the iframe but not its parent?

  3. 3

    how to change background color of Qwidget irrespective of its parents background color

  4. 4

    how can I get the menubar background color fit to the page?

  5. 5

    How can I get the background color of an HTML table's row?

  6. 6

    How to fill parent element background color with CSS?

  7. 7

    How can I make a button change its background color when pressed down?

  8. 8

    How can i change select box arrow when its have background color

  9. 9

    how to get the background color in vim?

  10. 10

    How to get an iframe to hold its position?

  11. 11

    Why when I create this new Bitmap its background is dark grey? How can I set it to the same color of the layout background?

  12. 12

    How to get the parent window URL from Iframe?

  13. 13

    How to get the parent window URL from Iframe?

  14. 14

    How to make width and height of iframe same as its parent div?

  15. 15

    How to copy a cloned node in an iframe to its parent with styles?

  16. 16

    Can't get the background color using DOM

  17. 17

    Child div needs to overlap on its parent such that it gets parent's background color

  18. 18

    Selector to get parent of <span> text to change its color

  19. 19

    Selector to get parent of <span> text to change its color

  20. 20

    ReactJS - How can a child find its parent?

  21. 21

    ReactJS - How can a child find its parent?

  22. 22

    How to run a program and get its PID in the background

  23. 23

    Given the following HTML and CSS how can I get the input elment to fill the width of its parent?

  24. 24

    How to make a div mask its parent's background

  25. 25

    uwp: how to change background color of listview item based on its value?

  26. 26

    How to change the background color of a tkinter Canvas after its' creation?

  27. 27

    How can I change background color for the desktop?

  28. 28

    How can I set Background Color for a Scene?

  29. 29

    How can I change background color for the desktop?

HotTag

Archive