How to provide html / js widget for users

How can I share html / js widgets? Javascript, iframe or which way is good?

Now I have an html page, css file and js file. I want to share this widget with people so that they can attach them to their blogs, websites, etc.

+4
source share
5 answers

iFrame for non-technical integration. That way you can simply offer them a piece of code to copy and paste.

+1
source

If your widget is a fixed size, then the iframe approach is probably the most straightforward. If you can offer it through a web service that can be called from a remote client, this is better from the developer's point of view, but an iframe is probably what you are looking for. It’s like regular banners, etc ..

I would suggest integrating any JS and CSS that you can add to your page to reduce remote calls if your widget hasn't changed much ... this will help to merge and at least merge and minimize your js and css into one file each.

+1
source

Here is a working example using java script.

contents of www.foo.com/test.js:

document.write('<p>some example content</p>'); 

copy / paste widget for test.js:

 <scirpt type="text/javascript" src="http://www.foo.com/text.js"></scirpt> 
+1
source

You might want to do something like this ...

AddWidget function (element) {

// Create your widget here

element.innerHTML = {your widget}

}

A function can be called like this:

addWidget (document.getElementById ("someID"));

0
source

See how Google does this with its latest asynchronous tracking code for Google Analytics. This is mainly due to the fact that your users embed a short piece of JavaScript code on their pages. This snippet dynamically inserts a <script> element into an HTML <head> element that points to a written JS file that creates the required HTML.

Google async code

0
source

Source: https://habr.com/ru/post/1303489/