Speed improvements on using the API

Following the story Chat-widget API sneak peek + small tutorial , we might explained it wrong, the way you use the API , or better said , based on the examples we didn’t show you the fastest way of using the API.
Let me explain what we’re talking about. We said in that post:

Well the key also hosts the name of the javascript variable that represents the widget. This variable is automatically created and you can implement it in you code only after you’ve embedded the code in you site or after the entire page has been loaded.

And this is true, but our examples were based on the second situation, and none of the first , which in this case is faster(not super fast, but a little faster).
So.. Instead of waiting for the entire page to load (using window.onload) before initializing your code, you can do it just after you embedded the widget like in this example:

<script type="text/javascript">// <![CDATA[
 document.write(unescape("%3Cscript src='" + document.location.protocol + "//www.hubtalk.com/default.php?page=widget5&w=w9359ab2efab9663ffa89bcb606f51ec7' charset='utf-8' type='text/javascript'%3E%3C/script%3E"));
// ]]></script>

var myWidget=w9359ab2efab9663ffa89bcb606f51ec7;
// do some stuff using myWidget
// or w9359ab2efab9663ffa89bcb606f51ec7

Notice that we removed the window.onload event, but we declared the api after the embedded code was inserted to your page.

If you want to use the window.onload event or <body onload=”//do something”> it doesn’t matter where you put the code, because it will be implemented only after the entire page is loaded.
You can also put it in a separate javascript file which is probably they way most of you’ll choose.

Hope this helps improve your code and make it as clean as possible.

Have fun coding

This entry was posted in Improvements, Tutorial and tagged , . Bookmark the permalink.

Comments are closed.