简体   繁体   English

如何允许我的页面的一部分用作其他站点上的 iframe?

[英]How to allow a part of my page be used as iframe on other site?

How can I allow a part of my website be embedded on other site?如何允许我的网站的一部分嵌入其他网站?

HTML: HTML:

 <div id="AllowToBeEmbedded"> <b>Hello there!</b> </div> <div class="nonsense"> <b>Leave me out of this</b> </div>

You could check if the page is rendered within an iframe or not, and if this is the case hide/remove parts of the page you don't want to be displayed.您可以检查页面是否在 iframe 中呈现,如果是这种情况,请隐藏/删除您不想显示的页面部分。

Typical way to check if the page is in iframe mode is by comparing current window object to a window.top reference:检查页面是否处于 iframe 模式的典型方法是将当前window对象与window.top引用进行比较:

if (window.top !== window) {
  // we are within an iframe. hide parts of the page
  hideForIframe()
}

function hideForIframe() {
  const toHide = document.querySelectorAll('.nonsense')
  for (let i = 0; i < toHide.length; i++) {
    toHide[i].parentNode.removeChild(toHide[i])
    // or hide toHide.style.display = 'none'
  } 
}

To avoid possible content flashing until it's removed it also would make sense to hide it additionally with CSS.为了避免可能的内容在被删除之前闪烁,使用 CSS 额外隐藏它也是有意义的。 For this I would have this helper code in the very beginning of the page (in <head> ):为此,我将在页面的最开头(在<head> )使用此帮助程序代码:

<script>
  const isInIframe = window.top !== window
  if (isInIframe) {
    document.documentElement.classList.add('iframe')
  }
</script>
<style>
  html.iframe .nonsense {
    display: none;
  }
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何限制我的网站被 ASP.NET 中的 iframe 从其他网站使用 - How to restrict my site to be used from other sites by iframe in ASP.NET 如何防止我的网站页面被加载到其他网站的 iframe 中? - How to prevent my site page from being loaded into other website iframe? 如果其他域使用iframe显示我的网站,如何重定向到错误页面 - How to redirect to error page if iframe is used by other domain to show my Website 如何检测我的网站在iframe中并删除iframe? - how to detect my site is in iframe and remove iframe? 不想在我的网站上显示 IFRAME KML 上使用的 LINK - Don't want to show LINK used on IFRAME KML the my site 为什么我的iFrame重定向到其他页面 - why my iFrame redirects to other page 除了AJAX和iframe,还有其他刷新页面一部分的方法吗? - Apart from AJAX and iframe, is there any other way to refresh part of a page? 滚动页面其他部分时需要iFrame不移动 - need iFrame to not move when other part of page is scrolled 如何允许iframe嵌入网页? (iframe显示空白屏幕) - How to allow iframe embed for a web page? (The iframe shows blank screen) 如何在我的扩展程序的新标签页中将其他扩展程序的新标签页加载为iFrame - How to Load other extension's new tab page as iFrame in my extension's new tab page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM