JavaScript – Hide/Unhide Entities With Scriptaculous

JavaScript is a scripting language widely used for client-side web development. It was the originating dialect of the ECMAScript standard. It is a dynamic, weakly typed, prototype-based language with first-class functions. JavaScript was influenced by many languages and was designed to look like Java, but be easier for non-programmers to work with. Although best known for its use in websites (as client-side JavaScript), JavaScript is also used to enable scripting access to objects embedded in other applications. JavaScript, despite the name, is essentially unrelated to the Java programming language, although both have the common C syntax, and JavaScript copies many Java names and naming conventions. The language was originally named LiveScript but was renamed in a co-marketing deal between Netscape and Sun, in exchange for Netscape bundling Sun’s Java run-time with their then-dominant browser. The key design principles within JavaScript are inherited from the Self and Scheme programming languages. JavaScript is a trademark of Sun Microsystems. It was used under license for technology invented and implemented by Netscape Communications and current entities such as the Mozilla Foundation


What Is Scriptaculous?

The Scriptaculous provides one with easy-to-use, cross-browser user interface JavaScript libraries to make web sites and web applications fly. It’s an add-on to the Prototype framework. Prototype is a JavaScript Framework that aims to ease development of dynamic web applications. Featuring a unique, easy-to-use toolkit for class-driven development and the nicest Ajax library around, Prototype is quickly becoming the codebase of choice for web application developers.


Why I Need It?

As part of my photoblog software, the entire page would reload [from scratch] when a user/visitor chose to display Details &/or EXIF Information. This had been bothering me for a while and not knowing enough JavaScript myself, it took me a while to figure things out [by which I mean take someone else’s code; learn just enough JavaScript to be dangerous; modify the code meet my requirements].


The Code

Download the scripts, prototype.js, builder.js, controls.js, dragdrop.js & scriptaculous.js and store them in a folder called scripts under the web root. If you cannot find them online for some reason, you can take mine:

prototype.js | builder.js | controls.js | dragdrop.js | scriptaculous.js



First step is to include these scripts within the HEAD section of the HTML/PHP file.

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- HEAD Section BEGINS-->
<head>
 
[MORE STUFF]
 
<!-- JavaScripts -->
<script type="text/javascript" src="/scripts/prototype.js"></script>
<script type="text/javascript" src="/scripts/scriptaculous.js"></script>
 
[MORE STUFF]
 
</head>
<!-- HEAD Section ENDS-->



Next step is to use Effects.toggle and one of the display options to get the desired effect while hiding / unhiding entities. The following will go in an appropriate location within the <body> element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- BODY Section BEGINS -->
<body>
[MORE STUFF]
 
<a href="#" onclick="Effect.toggle('toggle_stuff', 'blind'); return false;">Show My Stuff</a>
 
<div id="toggle_stuff" style="display:none">
  <div>
    STUFF THAT NEEDS HIDING / UNHIDING
  </div>
</div>
 
[MORE STUFF]
</body>
<!-- BODY Section ENDS -->

Please note that

  1. toggle_stuff is the name of the DIV ID which will contain the text that needs hiding / unhiding.
  2. blind is one possible option; appear and slide are other options.
  3. blind and slide take { delay: N } as an optional parameter; N is the number of seconds and it can be fractional.
  4. The toggle_stuff DIV ID must be followed by another DIV; else, the effect may not work as intended.
  5. Using display:none as style will hide the stuff when the page loads at first. Clicking once on the link above will show it. Clicking again will hide it.
  6. If there are more stuff, in different parts of the same page, that need toggle effect, they need to be within unique DIV ID. Using CSS, one can decorate these DIV ID elements with some jazzy effects.


Demonstration

One can check out this page and click on Details, Histogram, EXIF and/or Comments to see it in action.

8 Replies to “JavaScript – Hide/Unhide Entities With Scriptaculous”

  1. Hey, I found this blog article while searching for help with JavaScript. I have recently switched browsers from Safari to Mozilla Firefox 3.1. Now I seem to have a problem with loading JavaScript. Every time I browse page that needs Javascript, my computer doesn’t load and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any aid is greatly appreciated! Thanks

Comments are closed.