使用 javascript 为小部件编写 iframe - 最佳实践/限制? [英] Using javascript to write an iframe for a widget - best practices / limitations?
问题描述
我正在开发某种第三方小部件(HTML5/js 视频播放器),旨在无缝嵌入第三方网站,可与 Flash 播放器(HTML5/js 播放器在这种情况下是一个后备).
为了允许通过单个网关进行外部嵌入,我们提供了大致如下所示的嵌入代码:
<script src="http://example.com/embed.php?id=12345678&width=400&height=300"></script>
注意 .php 扩展名.这个脚本的作用是执行一些基本的浏览器代理检测,运行一些主要基于id"查询字符串的后端逻辑,然后将 document.write 的 javascript 回显到页面的实际嵌入代码.
如果是支持 Flash 的设备,document.write 代码是直接的 Flash 嵌入代码(无 iframe;直接到他们的页面);如果是 iOS 设备,它会写入
<iframe>
,其 src 指向一个 HTML 文档,该文档是 HTML5/javascript 播放器——在其中包含自己的 .js,body 中的一组
我相信这可以清理/做得更好.所以,我的问题:
可以或应该删除哪些步骤(如果有)?
开始时将 HTML5/js 版本放入 iframe 中而不是直接将其全部写入页面是个好主意吗?我们在
中使用的外部 .js 呢?此外,在未来,我们可能需要与我们的网站进行某种双向通信(与 iframe src 相同的域),因此 iframe 提供了一种很好的方式来实现这一点,对吗?
如果在 iframe 中加载,是否有任何我需要担心的范围问题(例如,我们播放器的 jQuery 版本与第三方父页面上的内容冲突)?
有什么我可以/应该做的事情来确保正确的加载顺序,这样我就不会在我们的脚本加载/执行时对第三方网站产生负面影响?
是否有比使用 document.write 将最终嵌入代码写入第三方页面(无论是 Flash 的
代码还是 HTML5/js 的
)?我应该将此代码写入我们包含在原始 js 行中的 div 中,还是仅将其 document.write 到它碰巧所在的任何容器中是否安全?
谢谢!Javascript 的作用域问题和异步行为从来都不是我的强项...
2,3) 如果使用 iframe,则必须将单独的 jQuery 库加载到该 iframe 中.这可能是使用 iframe 的充分理由(防止版本问题).在某些情况下,这样做的代价是不必要地下载两次 jQuery.
5) document.write
应始终避免使用 unclenorton 提到的原因.但是,允许用户在不需要调用任何 JS 的情况下嵌入它会很好.一种需要考虑的方法是将他们想要将玩家放入的元素的 id 传递到脚本标签中.
类似于:<div id='my-video'></div>
<script src="http://example.com/embed.php?id=12345678&width=400&height=300&nodeId=my-video"></script>
然后您的脚本可以挂接到 DOMReady/onload 事件并构建 HTML,而无需 document.write
使用
//这应该从一个 onload 处理程序调用document.getElementById('<?= $_GET['nodeId']?>').innerHTML = myHtmlString;
或者通过无聊的createElement
appendChild
调用
I'm in the middle of development of a third-party widget of sorts (an HTML5/js video player), designed to be embeddable on third-party sites seamlessly, which works in conjunction with a Flash player (the HTML5/js player in this case is a fallback).
To allow external embedding via a single gateway, we're offering embed code that looks roughly like this:
<script src="http://example.com/embed.php?id=12345678&width=400&height=300"></script>
Note the .php extension. What this script does is perform some basic browser agent detection, run a bit of backend logic based primarily on the 'id' query string, and then echo javascript that document.write's to the page the actual embed code.
If it's a Flash-enabled device, the document.write code is the straight <object>
Flash embed code (no iframe; straight to their page); if it's an iOS device, it writes the <iframe>
, whose src points to an HTML document that is the HTML5/javascript player -- with its own .js in the <head>
, a set of <div>
s in the body that make up the HTML5/js player, etc.
I'm sure this can be cleaned up / done better. So, my questions:
What steps, if any, can or should be removed?
Is it a good idea to begin with to drop the HTML5/js version into an iframe, rather than write it all to the page directly? What about the external .js that is needed, which we use in the
<head>
? Also, in the future, we're likely to need some 2-way communication with our site (same domain as the iframe src), so the iframe offers a good way to allow this, correct?Are there any scope issues I need to be worried about if loaded within an iframe (for instance, our player's version of jQuery conflicting with what's on the third-party parent page)?
Is there anything I can/should do ensure a proper load order so I don't negatively affect the third-party site while our script loads/executes?
Is there a better/smarter option than using document.write to write the final embed code to the third-party page (whether it's Flash's
<object>
code or the HTML5/js's<iframe>
)? Should I be writing this code to a div that we include with the original js line, or is it safe to just document.write it to whatever container it happens to be in?
Thanks! Javascript's scope issues and asynchronous behaviors have never been a strong point of mine...
2,3) If using an iframe, a separate jQuery library has to be loaded into that iframe. That may be good enough reason to use an iframe (prevent version problems). The cost of that is downloading jQuery twice unnecessarily in some cases.
5) document.write
should always be avoided for reasons mentioned by unclenorton. However, it'd be nice to allow the user to embed it without needing to call any JS. An approach to consider is to pass in the id of the element they want to put the player in to the script tag.
Something like:
<div id='my-video'></div>
<script src="http://example.com/embed.php?id=12345678&width=400&height=300&nodeId=my-video"></script>
Then your script can hook into the DOMReady/onload event and build the HTML without document.write
using
// This should be called from an onload handler
document.getElementById('<?= $_GET['nodeId']?>').innerHTML = myHtmlString;
Or through boring createElement
appendChild
calls
这篇关于使用 javascript 为小部件编写 iframe - 最佳实践/限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!