Addon JavaScript Code Snippets

These JavaScript scripts can be used in conjunction with other snippets to enhance their usefulness. These can save you a great deal of time. Many of these are generic and can be used in most snippets.

Results via Envato Market

JavaScript functions have a special property called arguments, which contains an array of input parameters. Using the ‘length’ property of an array, you can iterate through the array for each parameter. This enables you to develop functions that can change as their parameters do

There is a well-known bug in the Internet Explorer implementation of the getElementById() method, which, contrary to the W3C standard, allows the method to return an element even if only the element’s name attribute matches the requested id. This snippet will easily correct that problem.

This function splits any well-formed URI into its parts (all are optional). All parts are split with a single regex using backreferences, and all groupings which don’t contain complete URI parts are non-capturing.

Use this snippet in an external file to call several functions using the onLoad event handler.

JavaScript’s Array object only sorts alphabetically. That means if you pass it an array of numbers it will sort the array alphabetically (1,15,100,2,25,200) instead of numerically (1,2,15,25,100,200). This is easily fixed by adding a new method which will sort a numerical array very nicely.

When using strict doctypes the target attribute is not allowed for opening new windows. This script will accomplish that task easily and unobtrusively.

Use this script to determine the JavaScript key code of a selected key. Just press a key and an alert box will display the corresponding keycode.

Need to load several functions after the page loads? Use this snippet to call several functions using the onLoad event handler. Easy to use.

This script provides functions for browsers which do not inherently support the entire JavaScript Core and supplies some useful functions PHP developers might wish they had access to.

Here’s a trick that will redirect a browser when JavaScript is turned off. Why not redirect the ones that have JavaScript? Less than 10% of browsers have JavaScript turned off; therefore, redirecting less than 10% of visitors is better than redirecting over 90%.

When you start using Prototype, Scriptaculous, and other JavaScript libraries, you often end up with a dozen lines of script tags pulling in all of the code from different places. That would be fine if you never added a new library or never needed to swapped them out. This script will create a single JavaScript loader for each project that contains all of the scripts necessary.

This snippet searches an array for a pre-specified element and returns ‘true’ if it’s found, ‘false’ if it’s not found. It functions like the in_array() in PHP.