Web Analytics Made Easy -
StatCounter Combining Functions with Parameters? - CodingForum

Announcement

Collapse
No announcement yet.

Combining Functions with Parameters?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining Functions with Parameters?

    I have 3 functions in which 75% of their statements are the same. Can I combine them into 1 function using parameters? If so, how do I call a parameter from a link and define them in the function?


    I believe the function and links that call it would look something like this:

    function One(WhatDoIPutHere) {

    all common statements here;

    if (Link1) {
    do stuff;
    }

    if (Link2) {
    do stuff;
    }

    if (Link3) {
    do stuff;
    }
    }

    <a href="javascript:void(0);" onmousedown="One('Link1')">Link1</a>

    <a href="javascript:void(0);" onmousedown="One('Link2')">Link2</a>

    <a href="javascript:void(0);" onmousedown="One('Link3')">Link3</a>


    Thanks for your help.

  • #2
    Here is one way you can do it:

    function One(num) {

    all common statements here;

    if (num=='Link1') {
    do stuff;
    }

    if (num=='Link2') {
    do stuff;
    }

    if (num=='Link3') {
    do stuff;
    }
    }

    Comment


    • #3
      Thanks John.

      I understand what == does but what is "num"? Is it an abreviation for number?

      Comment


      • #4
        num was just an arbitrary variable name I choose. In my mind it did mean number but it really could be anything you want. Choose a vairbale name that makes sense to you. The variable name will represent the parameter that you passed in. It could just as easily been:


        function One(fredtheVariableIamIam) {

        all common statements here;

        if (fredtheVariableIamIam=='Link1') {
        do stuff;
        }

        if (fredtheVariableIamIam=='Link2') {
        do stuff;
        }

        if (fredtheVariableIamIam=='Link3') {
        do stuff;
        }
        }
        Last edited by JohnKrutsch; Jun 18, 2002, 11:42 PM.

        Comment


        • #5
          LOL thanks John.

          Comment

          Working...
          X