Results 1 to 2 of 2

Thread: Javascript - Switching DIV's

  1. #1
    Join Date
    Jul 2010
    Posts
    25

    Javascript - Switching DIV's

    Hi, just followed a tutorial which showed how to use javascript to switch between divs.

    Script seems to work fine, but theres a part i dont understand.

    function ShowContent(d,e) {
    if (d.length < 1) {return;} - Not sure what this line here does?
    document.getElementById(d).style.display = "block";
    document.getElementById(e).style.display = "none";
    }

    <a
    onclick="ShowContent('one','two'); return true;"
    href="javascript:ShowContent('one')">
    Page 1
    </a>

    Anyone care to enlighten me?

  2. #2
    Join Date
    Dec 2009
    Location
    San Diego, California
    Posts
    161
    If the length of the string (which is the variable "d" in your example) being passed in is less than 1 character long (in other words empty), return.

    "return" immediately stops the function it's inside of and returns the value after it. In the case of no value, it simply stops the function.

    Example:

    Code:
    function ShowMessage() {
         return;
         alert('Hi There');
    }
    The above example will never alert anything when you call ShowMessage() because it returns before you reach the alert;

    Code:
    function ShowMessage() {
         alert('Hi There');
         return;
         alert('I love Ice Cream');
    }
    The above example will alert "Hi There", but not "I love Ice Cream".

    Additionally,
    Code:
    if (d.length < 1) {return;}
    means the same thing as:
    Code:
    if (d.length < 1) {
         return;
    }
    Hope this answers your question.

Similar Threads

  1. JavaScript Collector
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 03:28 AM
  2. JavaScript library
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 01:26 AM
  3. element specific javascript
    By gumbo in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 04 Dec 2005, 06:25 PM
  4. element specific javascript
    By gumbo in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 04 Dec 2005, 06:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •