6

I am working on an app that will be deployed to Facebook therefore viewed from an iframe inside of Facebook's chrome.

I have some basic media queries that linearise the content at a set viewport size.

When the site is viewed in the browser locally the media queries work fine but when tested inside Facebook's chrome then they do not work.

I assume that the resizing of the viewport is not detected by the child of the iframe therefore the media queries will have no effect. Is there a way to get this working?

RyanP13
  • 7,413
  • 27
  • 96
  • 166

2 Answers2

3

As my adapted code was using media queries in a common CSS file, I prefer to toggle a fbIframe class on the body element, and add specific CSS rules in context of this class. The jQuery code thus becomes simpler:

function adaptToWindowSize(){
    $("body").toggleClass("fbIframe", $(window).width() < 820);
};
$(window).resize(adaptToWindowSize);
$(adaptToWindowSize);
Adrien Joly
  • 5,056
  • 4
  • 28
  • 43
3

You could add a little jQuery to detect a window resize and then swap out a separate stylesheet.

$(window).resize(function(){

    var currentWidth = $(document).width();
    var allStyleSheets = $("link");
    allStyleSheets.each(function(){ 
        var $this = $(this);

        //assume sheet1.css is for pages with widths equal and over 700 and sheet 2 is for widths under 700px
        if(currentWidth >= 700 && $this.attr("href").indexOf("sheet2.css") > 0){
            $this.remove();
            $(head).append('<link href="sheet1.css" type="text/css" rel="stylesheet" />');
        }
        if(currentWidth < 700 && $this.attr("href").indexOf("sheet1.css") > 0){
            $this.remove();
            $(head).append('<link href="sheet2.css" type="text/css" rel="stylesheet" />');
        }
    });    

});
Fisch
  • 3,775
  • 1
  • 27
  • 38