Back To Start Of Archive
Taken From The Forum: Archived Topics for the old Version 3.0 JavaScript Menu
Forum Topic: Click to view post
Last Updated: Wednesday July 18 2012 - 06:07:43
Tips for sharing scripts from an About.com article
Poster: damurphy
Dated: Friday April 5 2002 - 8:02:07 BST
Sharing Your Scripts
Tips on making your scripts more useful to others
So you just created the coolest script ever and you want to share it with the world or perhaps just your teammates. Before you hit the submit button here are a few suggestions and tips that will make it easier for others to reuse your script. And even if no words of undying gratitude show up in your inbox, you may just appreciate the extra effort yourself if you need to reuse it on another page in the future.
Objectify
If the script contains more than a couple of functions or global variables, you should consider turning it into a JavaScript object. As an object, you can associate data and functions with a single logical entity rather than a series of disconnected variables and global functions. What used to be held in global variables can be turned into properties of the object. That helps to protect that data from accidentally being overwritten by other scripts. For more information, see my article Object-Oriented JavaScript.
Identify Browser Compatibility
Potential users of your script are likely to have different browser requirements. Identify which versions of the browser (Netscape Navigator, Internet Explorer) and platforms (Windows, Macintosh) your script works on. If you do not have some of the browser versions or platforms, draft a friend or colleague to give it a quick try.
One or Two-Step Use
If your script requires a lot of setup or calls to multiple functions it raises the barrier for someone to easily use it. Design with the goal of reducing the number of steps it takes to incorporate the basic functionality. Consider packaging your script as an external ".js" file and making it possible for the functionality to be included directly in the HTML.
For example, suppose you have a reusable JavaScript clock that displays the current time. You could design the clock script so that the user simply includes the external file into the body of the page. The script would then be responsible for executing the necessary document.write calls to display the clock.
<BODY>
<SCRIPT language="JavaScript" src="clock.js"></SCRIPT>
</BODY>
If the script might be needed in several places on the page, design so that it can be included once in the HEAD and invoked later in the BODY with a single function call.
Customizable
Once a user sees how your script works and has found it easy to incorporate into their page, they will want to customize it next. Typically they will want to change fonts, colors, backgrounds, sizes, and so on. Use cascading style sheets (CSS) so that the user can easily supply their own style sheet to override original settings.
For settings not controlled by CSS, make them properties of your object. For example, suppose the clock script has a border color and a background image. Rather than hard coding these into the script, allow the user to change the properties when they create the object.
var myClock = new Clock();
myClock.backgroundImage = "images/clockbg.gif";
myClock.borderColor = "#FFAA00";
…