You are right that this is usually just a script that the client embeds on their site. However, that after that is a bit more complicated.
1. Insert a script
The first step, as said, is the script on the landing page.
This is essentially a script - it's just a piece of JavaScript code. This is very similar to what you would have on your own page.
This script should generate the content on the client page that you want to display.
However, there are some things to consider:
- You cannot use any libraries (or, if you want, be very careful what you use): they can conflict with what is already on the page and break the client’s site. You do not want to do this.
- Never redefine anything, as redefinition can violate the client site: this includes event listeners, properties of own objects, whatever. For example, always use
addEventListener
or addEvent
with events because it allows you to have multiple listeners - You cannot trust any styles: all the styles of the HTML elements that you create must be inlined because there can be a custom CSS style for the client website for them.
- You cannot add your own CSS rules: they can break the client site again.
These rules apply to any script or content that you run directly on the client’s site. If you create an iframe and show your content there, you can ignore these rules in any content inside the frame.
2. The script process on your server
An embeddable script should usually be a script created on your server. This allows you to enable logic, for example, selecting a display based on parameters or data from the application database.
It can be written in any language you like.
Usually your script URL should contain some sort of identifier so that you know what to display. For example, you can use an identifier to indicate which client site it is or something else.
If your application requires a login, you can process it in the same way as in normal mode. The fact that the server side of the script is being called by another site does not matter.
Communication between the embedded script and your server or frames
There are a few tricks in this.
As you know, XMLHttpRequest does not work in different domains, so you cannot use it.
The easiest way to send data from another site is to use an iframe and send the form to the user inside the iframe (or run XMLHttpRequest inside the frame, as the iframe content is on your own server, therefore it is not a cross-domain connection)
If your inline script displays content in the iframe dialog box, you may need to specify a script that is embedded in the client site when you need to close the iframe. This can be achieved, for example, 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