JavaScript Snippets - Page 3 | JavaScriptSource

JavaScript Snippets - Page 3

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.

  • Rock Solid addEvent()
    Apr 24, 2006

    Usually, when one event loads, it writes over any previous ones. Using this snippet, you can add several events to one page without any problems.

  • Popup Blocker Detection
    Casey Ryan Apr 7, 2006

    Use this snippet to detect if the client browser is using a popup blocker. If a one is detected, the script can then perform a different action. The sample uses an alert box to inform the user whether a popup blocker is present or not.

  • insertAfter()
    Mar 29, 2006

    Use this snippet to insert a node after another node. Quite handy in laying out Web pages.

  • Full Featured Browser/OS Detection
    Mar 10, 2006

    Add this code to your scripts when you need to detect the browser or operating system. It's quite complex but very effective.

  • Window Close Method
    Mar 3, 2006

    Use this script to close a popup window. Simple but effective!

  • Window Print Method
    Feb 23, 2006

    Add a button to allow your visitors to print your Web page. Compact and simple, yet highly requested.

  • getElementsByAttribute
    Feb 1, 2006

    Ever run into a situation where you want to get an array of all elements with a specific attribute? Or even want elements with a certain value for that chosen attribute as well? Just add this code snippet to your script and your problem will be solved.

  • Alert Box Protection
    Benjamin Joffe Jan 23, 2006

    Often when working on scripts alert boxes are placed inside loops as a debugging tool. You can sometimes end up with hundreds of alert boxes and no way to stop them. This script placed at the start of your code will give you an option to disable the alert boxes after a pre-determined number set by you.

  • Speed Up Object Detection
    Jan 11, 2006

    Object detection is common in JavaScript. Browser irregularities mean that your code must sometimes contain branches for different browsers. Use this snippet to perform object detection once instead of every time you call addEvent.

  • getElementsByClass
    Dec 30, 2005

    A quick and elegant way of grabbing elements by a class name. JavaScript provides functions for obtaining elements by id, name, and tag name, but not by class. This function will do the trick!

  • Prototype Dollar Function
    Dec 19, 2005

    The dollar function is a simple way to grab an element quickly. It not only takes in strings, it takes objects too.

  • Regular Expressions Taster
    Gilbert Hadley Dec 12, 2005

    Grab these regular expressions to use in your own JavaScripts. They can add another dimension to your programming experience.

  • Backward-Forward
    Lee Underwood Oct 24, 2005

    Use this snippet to add backward and forward buttons to your Web page.

  • Multiple onLoad
    Lee Underwood Sep 19, 2005

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

  • getPosition
    Aug 29, 2005

    When a layer is inside another layer, it's style.left and style.top positions can both be '0' but the layer itself can be 500 pixels away from the actual margin. These functions will return the true offset.

  • getElementById
    Aug 10, 2005

    The document.getElementById method is very useful but isn't supported in early browsers. To define the method for use in those older browsers, just add this snippet to the beginning of your scripts.

  • Window Open method
    Feb 23, 2002

    This tiny script will open as many windows as you choose to whatever sites you choose.

  • Alert Button
    Jun 21, 1997

    Sometimes you need to display an information box. Copy this script into your document and you will have a button alert. Label it whatever you want and customize the message to fit your needs.