Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Function within a Function (reusable code)?

Status
Not open for further replies.

rxsid

Programmer
Jul 9, 2002
57
US
Hi all,

I'm trying to call a function within a function, which I can do if I don't need it to return false. But I need it to return false (if it is)...how? I want to have this search.js file to be "re-usable" by two different search pages:


Code:
//*********************************
//**** INITIAL SEARCH CRITERIA ****
//*********************************
function initialSearch(sCat) {
    if (sCat == "_") {
        alert("some alert.");
        search.sCat.focus();
        return false;
    }
    return true;
}

//*******************************
//**** BASIC SEARCH CRITERIA ****
//this is called from search page 1
//*******************************
function searchBasic() {

    var sCat = search.sCat.value;
    initialSearch(sCat); //if initial check is false...don't go into code below (right now it does)
    
    if (search.sVar1.value=="_") {
        alert("some alert");
        search.sVar1.focus();
        return false;
    }

    if (search.sVar2.value=="_") {
        alert("some alert.");
        search.sVar2.focus();
        return false;
    }
    return true;
}

//***********************************
//**** ADVANCED SEARCH CRITERIA ****
//this is called from search page 2.
//***********************************
//work in progress...
function searchAdvanced() {
    
    return initialSearch();
    
    searchBasic(); //re-use basic logic  if any in basic logic fails...return false out of all code.
    
    //more complex checking in the works...

    return true;
}

Any ideas?

Thanks!
 
your initialSearch() returns boolean, so use it:
Code:
function searchBasic() {
	var sCat = search.sCat.value;

	if (initialSearch(sCat)) {
		if (search.sVar1.value=="_") {
			alert("some alert");
			search.sVar1.focus();
			return false;
		}

		if (search.sVar2.value=="_") {
			alert("some alert.");
			search.sVar2.focus();
			return false;
		}
	}
	return true;
}

also decide if you want searchBasic() to return true/false - currently it is always returning true

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
thanks for the reply jemminger....

when i put in your solution, if sCat == "_" is true...it does stop the user and display the alert.
however...when the ok button is pressed...the rest of the form is validated, even though search.sVar1.value=="_" && sVar2 are "_"
So those should return false...but it's not right now.

where is my logic falling through accidently here?
 
something more like this?
Code:
function searchBasic() {
    var sCat = search.sCat.value;

    if (!initialSearch(sCat)) {
      return false;
    }
    else {
        if (search.sVar1.value=="_") {
            alert("some alert");
            search.sVar1.focus();
            return false;
        }

        if (search.sVar2.value=="_") {
            alert("some alert.");
            search.sVar2.focus();
            return false;
        }
    }
    return true;
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top