JSONP vs IFrame? [英] JSONP vs IFrame?

查看:110
本文介绍了JSONP vs IFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为了将来验证我的小部件,嵌入代码将会是像这样:

 < script type =text / javascriptsrc =path / to / remote / file.js >< /脚本> 
< div id =my_widget>< / div>

iframe与JSONP的长处和短处是什么?

是否有任何常见的基于搜索引擎优化的iframes问题?首先,iframes和jsonp不是互斥的:一个是渲染的意思,另一个是沟通的意思。

您的选择是在文档内包含(即在主DOM内创建小部件)或in-iframe包含之间(即有一个新的单独的DOM



iframe的优势在于沙箱:您的小部件与主机的javascript和css之间没有冲突。这意味着您可以安全地使用/ b


  • 使用/定义任何您想要的JavaScript库

  • 使用简单的html代码连同简单的css规则(这对于维护来说是明显的奖励)



至于缺点:$ b​​
$ b


  • iframe的体重很重,可能会严重降低主机页面呈现效果

  • iframe也会消耗更多的内存和资源,如果主机页面是针对手机的话,这可能是个问题



因此,如果假设使用widget的人会合理愿意为自己的网页适应,采取文件内的方式。如果没有,请使用iframe但了解限制。



至于SEO问题,只要您动态创建窗口小部件(无论是在文档中还是在iframe中) ,搜索引擎将不会看到它。我不知道如果这就是你想要的,但这就是你会得到的;)


Soon I'll be needing to build a widget that some of our clients can embed in their own websites.

To future proof my widget the embed code would be something like this:

<script type="text/javascript" src="path/to/remote/file.js"></script>
<div id="my_widget"></div>

What are the strengths and weaknesses of iframes vs JSONP?

Are there any common SEO based issues with iframes?

解决方案

First of all, iframes and jsonp are not mutually exclusive: one is a rendering mean, the other is a communication mean.

Your choice is rather between in-document inclusion (that is creating the widget within the host DOM) or in-iframe inclusion (that is having a new, separate DOM for the widget).

The advantage of an iframe is sandboxing: no collision between your widget and the host's javascript and css. That means you can safely:

  • use/define any javascript library you want
  • use simple html code together with simple css rules (which is a clear bonus for maintenance)

As for the drawbacks:

  • an iframe is heavy-weight and may seriously slow down host page rendering
  • the iframe will also consume much more memory and resources, which may be a problem if the host page is targetted at mobiles

So, if it is reasonable to assume people using your widget will be willing to "adapt" their pages for it, go the in-document way. If not, use an iframe but understand the limits.

As for SEO issues, as long as you dynamically create the widget (whether it's in-document or with an iframe), search engines won't see it. I dunno if that's what you want, but that's what you'll get ;)

这篇关于JSONP vs IFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆