Javascript 书签的普遍接受的设计模式是什么?
是否最好插入一个 iFrame 并在其中执行任何需要执行的操作,以免页面因额外的脚本、CSS 样式等而变得混乱?当主窗口和 iFrame 之间无法通信时,如何检测事情已完成并删除 iFrame?
将脚本/CSS 插入 DOM 并在那里进行工作是否可以接受?
我的主要示例是 Instapaper 书签,它使用 iFrame 并在完成后将其关闭。
Is it best to insert an iFrame and do whatever needs to be done in there, so as to not clutter the page with your extra scripts, CSS styles, etc? How do you detect that things are complete and remove the iFrame when you can't talk between the main window and the iFrame?
Is it acceptable to insert scripts/CSS into the DOM and do your work there?
My main example is the Instapaper bookmarklet, which uses an iFrame and closes it when it's all done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做过的所有书签都是直接插入到主页的 DOM 中的。所有代码都位于该小书签的命名空间内,因此 DOM 污染保持在最低限度,当然,还会进行检查以确保与现有变量名称不发生冲突。
插入到主文档的 DOM 中的原因是因为它使一切变得更容易 - 很少有任何充分的理由让你自己变得更加困难:)
但这当然取决于你的目标。
All the bookmarklets i have ever done are directly inserted into the DOM of the main page. All the code has lived inside a namespace for that bookmarklet so the DOM pollution is kept at a minimum, and of course there are checks to make sure there are no collisions with existing variable names.
The reason for insertion into the DOM of the main document is because it makes everything easier - there is rarely any good reason to make it harder on yourself than it has to be :)
But of course this depends on your goals.
模式 1:
任何小书签返回值都会转换为 String 并按照传递给 document.write 的方式进行操作
void
运算符阻止它模式 2:
小书签在全局范围内运行,因此如果没有匿名函数,上面的代码将“泄漏”循环计数器为
window.i
Pattern 1:
any bookmarklet return value gets converted toString and act as it was passed to document.write
void
operator prevents itPattern 2:
bookmarklets are running in the global scope, so without anonymous function code above will "leak" loop counter as
window.i