Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic function

    Hi,

    I want to create a dynamic function e.g

    I have a global function called onSuccess but contents of the function need to be different depending on certain factors e.g

    var onSuccessFun;
    if (myvar = 'apple'){
    onSuccessFun = function(){
    alert("apple")
    }
    }else{
    onSuccessFun = function(){
    alert("grape")
    }
    }

    function onSuccess{
    onSuccessFun();
    }
    but this dosen't work?

    thanks for your help

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,950
    Thanks
    9
    Thanked 724 Times in 718 Posts
    Well this does what you're describing, although without knowing the wider context it's hard to know what you're trying to do.

    Code:
    function alertFruitName(){
    
        if(myvar == 'apple'){
            
            alert ('apple');
    
        } else {
    
    
           alert ('grape')
    
        }
    
    }
    
    myvar = 'banana';
    alertFruitName();//alerts 'grape'
    myvar = 'apple';
    alertFruitName();//alerts 'apple'
    You could also pass a value as an argument to the function:

    Code:
    function alertFruitName(fruit){
    
           alert (fruit);
    
    }
    
    alertFruitName('grape');//alerts 'grape'
    alertFruitName('banana');//alerts 'apple'
    Use the W3C HTML Validator and CSS Validator to check your code and Firebug to see what css is applied to an element
    Read Steve Krug's book Don't Make Me Think - essential reading on web usability
    I don't recommend much, but I do recommend Clook for UK web hosting


  •  

    Posting Permissions

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