【问题标题】:Hide header if in iframe如果在 iframe 中,则隐藏标题
【发布时间】:2014-02-05 17:47:06
【问题描述】:

我需要隐藏基于 wordpress 的网站的标题,以防网站加载到 iFrame 中。 我应该用javascript、函数还是css来做?我该怎么做? 我找到了这个

<script type="text/javascript">
   var isInIFrame = (window.location != window.parent.location);
   if(isInIFrame==true){
       alert("It's in an iFrame");
       document.getElementById('header').style.display = "none";
       }
   else {
       alert("It's NOT in an iFrame");
       }
   </script>

但我不知道把它放在哪里(而且我也不知道它是否可以工作)。 希望有人能帮帮我,谢谢!

【问题讨论】:

    标签: wordpress iframe header


    【解决方案1】:

    我从这篇文章中获取了这段代码(并对其进行了修改以供您使用),所以去那里给 Greg 一个 upvote :) How to identify if a webpage is being loaded inside an iframe or directly into the browser window?

    function inIframe() {
        try {
            return window.self !== window.top;
        } catch () {
            return true;
        }
    }
    
    if(inIframe){
        document.getElementById('header').style.display = "none"; //if you want javascript only
        $("#header").hide(); // if you want jquery
    }
    

    但是,这只有在标头的 ID 为“header”时才有效。

    【讨论】:

    • 我设法让它工作但现在我有一个任意的 57px 填充,而不是标题。这是输出结果
    • $("#header").hide().css('padding-top', 0);将删除填充。
    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多