Override body style for content in an iframe

前端 未结 10 1206
青春惊慌失措
青春惊慌失措 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:42

    If you have control of the page hosting the iframe and the page of the iframe, you can pass a query parameter to the iframe...

    Here's an example to add a class to the iframe based on whether or not the hosting site is mobile...

    Adding iFrame:

    var isMobile=$("mobile").length; //detect if page is mobile
    var iFrameUrl ="https://myiframesite/?isMobile=" + isMobile; 
    
    $(body).append("
    "); $("#wrapper iframe").attr("src", iFrameUrl );

    Inside iFrame:

    //add mobile class if needed
    var url = new URL(window.location.href);
    var isMobile = url.searchParams.get("isMobile");
    if(isMobile == "1") {
        $("body").addClass("mobile");
    }
    

提交回复
热议问题