javascript embed-able widgets

Learn how to create Javascript Embed-able Widgets without depending on external services

In our today tasks we have used to deal with external apis in facebook or twitter, But how to build something like this, an Api or Widget that works without using external dependencies or libraries.

 

 

In this tutorial you will learn how to build an embed-able Javascript plugin or API or in some cases called (Widgets), that can be injected to any website and work without using any external dependencies or libraries. For the purposes or this tutorial you must have a basic knowledge of frontend development using javascript, html and css and also backend development using any language in my case i will use PHP.  

 

Prerequisites

  • Http server with backend language (PHP for example)
  • Knowledge with backend and frontend develpment.

 

Before diving into creating an embed-able javascript plugin you must follow specific convention for including the plugin script file. Let’s imagine this example from the Facebook SDK, where it outputs it’s script using the below code:

This code seems confusing but if you understand it line by line you will get the idea, this example actually just inserting a script tag into the page using a dynamic way so it’s equivalent to this line:

 

So how this code works, let’s simplify the above code into a simple Javascript function so it will be equivalent to this code:

The function takes three parameters document which represents the document object, script which represents the html <script> tag,  facebook-jssdk which is the id of the <script> tag. The first line retrieves all elements with tag name script:

Then it checks if there is any tag in the document with this id, it will return otherwise will continue. It’s important to give your script a unique id and be sure that it will not conflict with other ids in the document.

The third line it creates the script tag and assign it the id and src attributes like this:

and finally the most important line which to output the script into the page before any other scripts:

If you want you can also control to output your script in the head or footer of the page or after or before any other elements,

 

Advantages

  • The first advantage of writing a Widget without depending on external library is that you can use anywhere on any compatible browser without need to import any external dependency like jQuery.
  • Be sure that the plugin will work on almost browsers and platforms.
  • Run on any browser either desktop or mobile because you don’t depend on specific platform.
  • Can be customized with a lot of options to change appearance and behavior and an easy way.

 

Disadvantages

  • Needs an extensive testing to be sure that it’s working on almost every desktop and mobile browser.

 

Now let’s put these concepts in action using a real world example, in this example we will create an embed-able widget. This widget is a simple box you can of it to be shown on sidebar or where ever and it will grab the latest xml feeds of any website and can be used as embeded widget to be used in other websites.

 

Widget Architecture

javascript widget architecture



As shown in the above figure the Javascript widget have a set of components:

  • The main javascript code: which is responsible for handling the widget logic and functionality.
  • Server code: the widget may need to send and receive data from server in order to finish it’s functionality.
  • Webpage: which you will insert this widget in.
  • Calling the javascript Api: which includes importing the javascript file as mentioned in above steps and calling the Widget Api.

 

Preparing Widget

Let’s create a new directory in your server webroot (htdocs) call it what ever you want, then create the following files in it:

javascript widget structure

 

 

 

 

 

The js/widget.js will contain the main javascript code, fetch.php will be used to fetch the remote xml file contents, we use server side code here for this functionality in order to avoid problems such as cross domain requests. index.html will be used to import and render the widget code.

 

index.html

js/widget.js

As shown above in the index.html we just imported the javascript file. We will come back to this file later after we finish the widget.js

 

In the main javascript file we added an object called XmlFeedGrabber, this object will hold the widget code and here we added a couple of functions and properties which are options which will hold the widget options like feed url, number of items etc,

targetHtmlEle represent the html element that this widget will be rendered in. messages() represent the error messages. themes()   function return the available themes of this widget, setOptions() functions allows you to set any of the widget options.

validate() function will validate the widget, fetch() function will be used to send and fetch the xml response from the server, template() function will return the widget template that will be rendered using the render() function.

 

Adding Widget Options and Messages

Let’s add the widget options and how to manipulate those options so open js/widget.js and modify it like shown below:

We just added some options to control the widget also called the default options like feed url, items count, target Id which represents the element id to be inserted on, showMedia to enable or disable showing media and others.

As a rule of thumb every javascript embed-able widget must have options, this enable users to switch between the different behaviors of the widget, for example like the facebook box widget.

In the setOptions() functions we check if any option passed then we update that option.

 



Validating the widget options

To validate the widget we updated the validate() function as shown below:

The above function checks for some options to be sure it exists otherwise it won’t work like the feed url, and target element then we output some messages in the console about that error and return false.

It’s a best practice to show error on the browser console and not in alert box, this is a common behavior in embed-able widgets.

 

Rendering the widget

Let’s render our widget so we will update the render(), fetch(), template(), and applyCss() functions as shown below:

In the above code in the render() function we first called the validate() function, then if everything ok then we apply the widget CSS by calling applyCss() function which renders some Css rules into the page head.

After that we called the fetch function which do an Httpxmlrequest to server sending a post request to the fetch.php file which we update it below. Then on successful response we called the template() function on the target element which takes the response and apply it to specific template and return it.

 

Widget Themes

Modify the themes() function to be like this:

 

 

Widget Backend

Open fetch.php and update it like this:

In the above php code we make a remote http request using CURL and then after receiving the response we manipulate the xml file using php SimpleXmlElement which processes the xml file and loops over xml items and returning it as a json response.

 

Calling the Widget

Now let’s try our widget with a sample xml feed i used the webmobtuts website feed url, so open index.html and update it as shown:

All we have done is calling the widget on window.onload() function then we passed the feedUrl and called render().

 

Demo

Download the source code

Share this: