JavaScript Snippets - Page 2 | JavaScriptSource

JavaScript Snippets - Page 2

These JavaScript snippets can be used in conjunction with other scripts to enhance their usefulness. These can save you a great deal of time. Many of these are generic and can be used in most scripts. General questions about JavaScript snippets here should be posted to The JavaScript Forum.

  • getElementsByClassName Deluxe Edition
    Stuart Colville Aug 8, 2007

    JavaScript does not provide a function for obtaining elements by class, only by id, name, and tag name. This snippet will accomplish that, and includes optional arguments with defaults and support for multiple classnames in any order. It works in IE, Firefox, Opera, Safari and IE Mac.

  • Display Page URL
    Lee Underwood Aug 6, 2007

    Sometimes you may want to give your visitor the URL of the page they are on. You can provide the entire path with breadcrumb scripts but, if you just want to give them the current URL, this short snippet will do the trick. Just place it anywhere on the page you want to display the URL.

  • Explorer Plugin Starter
    Jeroen Haan Aug 3, 2007

    A simple script which activates objects in Internet Explorer 7 so users don't have to click on Flash to activate. In addition to object and embed tags, support is also provided for applet tags and all their param tags.

  • Multiple onLoad 2
    Simon Willison Jun 29, 2007

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

  • History Keeper
    Kevin Newman Jun 8, 2007

    A JavaScript-based library for managing browser history (back button) and providing support for deep linking for Flash and Ajax applications.

  • JavaScript Includes
    Jun 1, 2007

    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.

  • JavaScript Utilities Library
    Aaron Gustafson May 18, 2007

    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.

  • Removing Nodes Using the DOM
    May 4, 2007

    You can remove existing nodes using the DOM. The removeChild method allows any node to remove one of its child nodes. Simply pass a reference to the node you wish to remove. Any text or elements within the node being removed will be removed along with it.

  • Numeric Array Sort
    Patrick Hunlock Mar 21, 2007

    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.

  • domFunction
    James Edwards Feb 26, 2007

    An easy-to-use snippet, that allows other DOM scripting to run before window.onload. The practical benefit is that JavaScript doesn't have to wait for images or other dependencies to finish loading. It can begin as soon as the DOM is ready.

  • Remove HTML Tags
    Robert Nyman Feb 5, 2007

    This function will remove all HTML tags from a string.

  • Convert HTML Entities
    Jan 29, 2007

    This script will convert entities (e.g., &, é) back into normal characters (e.g., &, xE9). Short and easy to use.

  • Explorer Object Starter
    Jeroen Haan Jan 22, 2007

    A very simple script which activates objects in Explorer 7 so people don't have to click on Flash to activate.

  • addLoadListener
    Jan 8, 2007

    This snippet will prevent designated functions from being executed until after the page loads. Includes a sample of the function in use.

  • Popup Blocker Detection 2
    Jeff Johnson Dec 20, 2006

    This is a simple, cross-browser method for testing for pop-up blockers. Most scripts test the return value of window.open and conclude that there is a blocker installed if the returned value resolves to false in a boolean statement. However, these scripts fail with browsers such as Opera (as of v9) that return a window object but disable it. This script properly handles this, as well as a null or undefined return type.

  • Opening New Windows
    Roger Johansson Dec 4, 2006

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

  • File Include
    Krijn Hoetmer Nov 27, 2006

    Use this script to target which files you will include on a designated Web page. Easy to set-up and use.

  • Add/Remove an Event
    Caleb Duke Nov 17, 2006

    Use this set of snippets to add or remove events from your scripts. Consists of two functions: addEvent and removeEvent.

  • getXML
    Oct 13, 2006

    Obtain the contents of an XML element and convert it to text. This script is great for using with Ajax-type applications, when it's neccessary to send back the contents of an element to the server without knowing what's there.

  • Obtaining the URL of the Referring Document
    Chris Stimpert Oct 6, 2006

    Use this snippet to obtain the URL which referred the user to that page. Be aware that on some servers this may not work.

  • Passing Arguments
    William Wen Aug 28, 2006

    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

  • JavaScript Off Redirect
    Jul 19, 2006

    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%.

  • Multi-Dimensional Array Searching
    Jun 5, 2006

    Use this prototype function to search through a multi-dimensional array. Simple yet efficient! Includes examples.

  • getStyle
    May 8, 2006

    Use this snippet to get the rendered style of an element. Quick and simple to implement.

  • Window Close Automatically
    Apr 28, 2006

    Many times you just want a popup window to stay open for a few seconds, or a bit longer. Place this snippet in the body tag of the popup window.