-1

I need to develop a web application, where our data/html need to be displayed on third party sites using iframe or javascript. Example: cricket widget sharing.

Can someone tell me what type development is it called ?

There would be multiple kind of widget, some of them will also need to be upgrded short periodically (per x second).

Also, should i use iframe or use javascript implemenration merhod to generate the output on clients server.

Can someone provide me a reference or idea ?

Shazzad
  • 950
  • 11
  • 20
  • Anyone down voted the question, could you explain why ? no offense, just want to understand what's wrong :) – Shazzad Mar 03 '16 at 02:32

1 Answers1

6

Considering I have understood your question rightly, which means you currently need to develop a widget(not a web app) for websites. A widget is something that can be used by others on their websites.

Adding to the above understanding with the example you gave-

Say you develop a Cricket widget, 100's of other websites can put this widget on their site using a small piece of API or code. And, this widget refreshes every 'x' seconds to display Live Score. Hope this is what you need.

Here is the solution/way to solve this:

What you have to do is write an embedder or loader script keeping the following points in mind-

  1. Make it asynchronous, so that the client website doesn't slow down

  2. Keep it short. Abstract your code. Don't give the user 100's of lines.

  3. Preferably don't use a framework. Chances are it can conflict with your client's website/frameworks. Don't even use jQuery!(Because it can conflict with user's jquery version and cause lot of problems to the widget and website) Write pure Javascript code :)

  4. Don't ever use GLOBAL variables. Use var or let and follow best practices. Using global variables may conflict with user's variables and the whole website/client can get messed up.

A sample code -

<script data-version='v1' data-widget-id='your-clients-id' id='unique-embedder-id' type='text/javascript'>
  // "data-version": It's useful to put the version of your embedder script in the "version" data attribute.
  // This will enable you to more easily debug if any issues arise.
  // "data-widget-id": This ID allows us to pull up the correct widget settings from our database.
  // It's a "client id" of sorts.
  // "id": This HTML id allows us to refer to this embedder script's location. This is helpful if you want to inject html
  // code in specific places in hour hosts's website. We use it to insert our payload script into the <head> tag.

  //We wrap our code in an anonymous function to prevent any ofour variables from leaking out into the host's site.
  (function() {
    function async_load(){
      //BELOW: we begin to construct the payload script that we will shortly inject into our client's DOM.
      var s = document.createElement('script');
      s.type = 'text/javascript';
      s.async = true;
      // Below is the URL you want them to get your payload script from!
      var theUrl = 'http://www.your-website.com/assets/your-payload-script.js';
      // At the end we tack on the referrer's URL so we know which website is asking for our payload script.
      s.src = theUrl + ( theUrl.indexOf("?") >= 0 ? "&" : "?") + 'ref=' + encodeURIComponent(window.location.href);
      // This finds the location of our embedder script by finding the element by it's Id.
      // See why it was useful to give it a unique id?
      var embedder = document.getElementById('unique-embedder-id');
      // This inserts our payload script into the DOM of the client's website, just before our embedder script.
      embedder.parentNode.insertBefore(s, embedder);
    }

    // We want the our embedder to do its thing (run async_load and inject our payload script)
    // only after our client's website is fully loaded (see above).
    // Hence, we wait for onload event trigger. This no longer blocks the client's website from loading!
    // We attachEvent (or addEventListener to be cross-browser friendly) because we don't want to replace our
    // client's current onLoad function with ours (they might be doing a bunch of things on onLoad as well!)
    if (window.attachEvent)
      window.attachEvent('onload', async_load);
    else
      window.addEventListener('load', async_load, false);
    })();
</script>

Read the comments. They are very helpful :)

Your users will have to just use a nicely abstracted script tag-

<script type="text/javascript" src="http://example.com/widget.js"></script>

Or maybe even an iFrame(old is gold days) render HTML in iFrame(Best way if you don't want to 'openly' expose(as most won't know) your Abstracted Javascript as well)

<iframe src="http://example.com/mywidget.html"></iframe>

Here are the references that I referred and also have sample widgets which can be reused-

  1. Developing an Embeddable Javascript Widget / Snippet for Client Sites

  2. Creating an Embeddable JavaScript Widget

  3. Creating Asynchronous, Embeddable JavaScript Widgets

  4. Building an embeddable Javascript widget (third-party javascript)

One last time, please don't use global variables and write asynchronous code. These 2 are the main things that you have to take care to make a great/successful widget with top-notch performance.

Happy Coding! Hope it helps :)

bozzmob
  • 12,364
  • 16
  • 50
  • 73
  • Thank you very much @bozzmod, it helped a lot. I will definitely not use any (js/css) framework if i am using javascript embed method. Only one question i have left in mind is, do i use iframe or generate template on the fly using JavaScript (if considering speed/performance) ? I don't have problems regarding security, i just need it to be fast. – Shazzad Jan 20 '16 at 22:38
  • 2
    I see it in 2 aspects- 1. If you use iFrame, even if your widget/html gets slow, its fine, it wont affect the main webpage loading time. As iframe loads separately. 2. If you use JS, then you need to maintain your server pretty well. Look and UI wise, you can work on both to get the best UI. It's all upto you :) I suggest, go with Template generation with JS executing at run-time. – bozzmob Jan 21 '16 at 09:19
  • 2
    Also, if you are going with template, then, please follow the steps I have given, else, JS can play really a bad game. With iFrame such tensions are less. – bozzmob Jan 21 '16 at 09:27
  • Thanks for your extreme explanation and walk-through. It would help me a lot. One last question, do you know how can i treat iframe as like a div tag ? so that it takes it's height/width as like a div tag, inheriting from parent element. – Shazzad Jan 26 '16 at 05:59
  • 1
    Have a look at - http://stackoverflow.com/questions/16890025/content-inside-iframe-match-width-of-parent-div , http://stackoverflow.com/questions/15739283/set-width-to-iframes-parent-div-tag , http://stackoverflow.com/questions/18765762/how-to-make-width-and-height-of-iframe-same-as-its-parent-div – bozzmob Jan 26 '16 at 18:20