KissInsights 和 Hello Bar 等基于 JavaScript 的模式/弹出服务如何工作?
我正在开发一个模式/弹出系统,供用户嵌入他们的网站,类似于 KissInsights 和 Hello Bar (示例此处和此处)即可。
构建这样的服务的最佳实践是什么?看起来用户嵌入了一些 JS,但该代码随后插入了额外的脚本标记。
我想知道它如何与网络服务通信以获取用户的内容等
。TIA
I'm developing a modal/popup system for my users to embed in their sites, along the lines of what KissInsights and Hello Bar (example here and here) do.
What is the best practice for architecting services like this? It looks like users embed a bit of JS but that code then inserts additional script tag.
I'm wondering how it communicates with the web service to get the user's content, etc.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是对的,通常这只是客户嵌入到其网站上的脚本。然而,接下来的事情就有点复杂了。
1. 嵌入脚本
如上所述,第一步是在目标页面上放置一个脚本。
本质上,这个脚本只是一段 JavaScript 代码。它与您自己页面上的内容非常相似。
该脚本应在客户页面上生成您希望显示的内容。
但是,您需要考虑一些事情:
addEventListener
或addEvent
与事件一起使用,因为它们允许您拥有多个侦听器这些规则适用于您在客户站点上直接运行的任何脚本或内容。如果您创建 iframe 并在其中显示内容,则可以在框架内的任何内容中忽略这些规则。
2. 在服务器上处理脚本
您的嵌入脚本通常应由服务器上的脚本生成。这允许您包含逻辑,例如根据参数或应用程序数据库中的数据选择要显示的内容。
这可以用您喜欢的任何语言编写。
通常,您的脚本 URL 应包含某种标识符,以便您知道要显示什么。例如,您可以使用 ID 来判断它是哪个客户的站点或其他类似信息。
如果您的应用程序要求用户登录,您可以像平常一样处理。服务器端脚本被其他网站调用这一事实没有什么区别。
嵌入式脚本与服务器或框架之间的通信
也有一些技巧。
您可能知道,XMLHttpRequest 不能跨不同域工作,因此您不能使用它。
从其他站点发送数据的最简单方法是使用 iframe 并让用户在 iframe 内提交表单(或在框架内运行 XMLHttpRequest,因为 iframe 的内容驻留在您自己的服务器上,因此没有交叉域通信)
如果您的嵌入脚本在 iframe 对话框中显示内容,您可能需要能够告诉嵌入在客户站点上的脚本何时关闭 iframe。例如,可以通过使用
window.postMessage
来实现。对于 postMessage,请参阅 http://ejohn.org/blog/cross-window-messaging/
对于跨域通信,请参阅 http://softwareas.com/cross-domain-communication-with-iframes
You're right that usually it's simply a script that the customer embeds on their website. However, what comes after that is a bit more complicated matter.
1. Embed a script
The first step as said is to have a script on the target page.
Essentially this script is just a piece of JavaScript code. It's pretty similar to what you'd have on your own page.
This script should generate the content on the customer's page that you wish to display.
However, there are some things you need to take into account:
addEventListener
oraddEvent
with events, because these allow you to have multiple listenersThese rules apply to any script or content you run directly on the customer site. If you create an iframe and display your content there, you can ignore these rules in any content that is inside the frame.
2. Process script on your server
Your embeddable script should usually be generated by a script on your server. This allows you to include logic such as choosing what to display based on parameters, or data from your application's database.
This can be written in any language you like.
Typically your script URL should include some kind of an identifier so that you know what to display. For example, you can use the ID to tell which customer's site it is or other things like that.
If your application requires users to log in, you can process this just like normal. The fact the server-side script is being called by the other website makes no difference.
Communication between the embedded script and your server or frames
There are a few tricks to this as well.
As you may know, XMLHttpRequest does not work across different domains, so you can't use that.
The simplest way to send data over from the other site would be to use an iframe and have the user submit a form inside the iframe (or run an XMLHttpRequest inside the frame, since the iframe's content resides on your own server so there is no cross domain communication)
If your embedded script displays content in an iframe dialog, you may need to be able to tell the script embedded on the customer site when to close the iframe. This can be achieved for example by using
window.postMessage
For postMessage, see http://ejohn.org/blog/cross-window-messaging/
For cross-domain communication, see http://softwareas.com/cross-domain-communication-with-iframes
您可以查看此处 - 这是使用我的 JsApiToolkit,一个允许服务提供商轻松创建类似 Facebook Connect 的工具并将其分发给第三方的框架网站。
该库构建于用于跨域消息传递的 easyXDM 之上,并通过模式对话框或弹出窗口促进交互。
代码和自述文件应该足以解释事物如何组合在一起(一旦您抽象出 XDM 之类的东西,它实际上并不太复杂)。
关于嵌入本身;您可以直接执行此操作,但大多数服务使用“引导”脚本,可以轻松更新以指向真实文件 - 这个小文件可以使用缓存编译指示来提供服务,以确保它不会缓存太长时间,而注入的文件可以作为长期存在的文件。
这样,您只会产生重新下载引导程序的开销,而不是整个脚本集的开销。
You could take a look here - it's an example of an API created using my JsApiToolkit, a framework for allowing service providers to easily create and distribute Facebook Connect-like tools to third-party sites.
The library is built on top of easyXDM for Cross Domain Messaging, and facilitates interaction via modal dialogs or via popups.
The code and the readme should be sufficient to explain how things fit together (it's really not too complicated once you abstract away things like the XDM).
About the embedding itself; you can do this directly, but most services use a 'bootstrapping' script that can easily be updated to point to the real files - this small file could be served with a cache pragma that would ensure that it was not cached for too long, while the injected files could be served as long living files.
This way you only incur the overhead of re-downloading the bootstrapper instead of the entire set of scripts.
最佳实践是在代码片段中放入尽可能少的代码,这样您就不必要求用户更新他们的代码。例如:
如果作者将其嵌入到他们的页面中,则效果很好。否则,如果您需要小书签,您可以使用此代码在任何页面上加载脚本:
现在您的所有代码都将位于上面引用的 URI 中,并且每当加载其页面时,都会下载代码的新副本并被执行。 (不考虑缓存设置)
在该脚本中,只需确保不会破坏名称空间,并在加载另一个库之前检查库是否存在。如果您正在使用安全的
jQuery
对象而不是$
。如果您想加载更多外部内容(例如 jQuery、UI 内容等),请使用 onload 处理程序来检测它们何时完全加载。例如:那么你可以简单地调用这个函数来加载任何js文件,并执行回调函数。
现在,与域通信以检索数据的一种棘手方法是使用 javascript 作为传输。例如:
您的服务器必须动态处理该路由,并返回一些具有“getSome”函数的 javascript,该函数可以执行您想要的操作。例如:
这将非常有效地允许您与服务器通信并从服务器可以获得的任何地方处理数据。
Best practice is to put as little code as possible into your code snippet, so you don't ever have to ask the users to update their code. For instance:
Works fine if the author will embed it inside their page. Otherwise, if you need a bookmarklet, you can use this code to load your script on any page:
Now all your code will live at the above referenced URI, and whenever their page is loaded, a fresh copy of your code will be downloaded and executed. (not taking caching settings into account)
From that script, just make sure that you don't clobber namespaces, and check if a library exists before loading another. Use the safe
jQuery
object instead of$
if you are using that. And if you want to load more external content (like jQuery, UI stuff, etc.) use theonload
handler to detect when they are fully loaded. For example:Then you can simply call this function to load any js file, and execute a callback function.
Now, a tricky way to communicate with your domain to retrieve data is to use javascript as the transport. For instance:
Your server will have to dynamically process that route, and return some javascript that has a "getSome" function that does what you want it to. For instance:
That will pretty effectively allow you to communicate with your server and process data from anywhere your server can get it.
您可以从您的服务器提供动态生成的(例如使用 PHP 或 Ruby on Rails)在每个请求上生成此文件)JS 文件,该文件是从客户网站导入的,如下所示:
然后您需要为您的客户提供一种方法决定他们希望模式/弹出窗口包含什么内容(例如文本、图形、链接等)。您可以创建一个简单的 CMS,也可以为每个客户手动执行。
你的服务器可以看到每个 JS 文件的请求来自哪里,并据此提供不同的 JS 代码。例如,JS 代码可以将 HTML 代码插入到您的客户网站中,在顶部创建一个包含一些文本和链接的栏。
如果您想访问客户访问者信息,您可能需要从 HTML 代码中读取该信息,让您的客户以特定方式提供您想要的信息,或者找出从每个客户的 Web 服务器访问该信息的不同方式。
You can serve a dynamically generated (use for example PHP or Ruby on Rails) to generate this file on each request) JS file from your server that is imported from the customers web site like this:
Then you need to provide a way for your customer to decide what they want the modal/popup to contain (e.g. text, graphics, links etc.). Either you create a simple CMS or you do it manually for each customer.
Your server can see where each request for the JS file is coming from and provide different JS code based on that. The JS code can for example insert HTML code into your customers web site that creates a bar at the top with some text and a link.
If you want to access your customers visitors info you probably need to either read it from the HTML code, make your customers provide the information you want in a specific way or figure out a different way to access it from each customers web server.