www.webdeveloper.com
Results 1 to 3 of 3

Thread: Need to make a "generic" function

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    Need to make a "generic" function

    Hello all,
    I am using the YUI framework and have a set of functions that all do the same thing just with different variables. Hw can I make these functions into a single function that takes its value from the 'a' tag's href attribute?

    Instead of this :<li><a href="javascript:send_aboutUsRequest();">About Us</a></li>

    I would like to use something like this: <li><a href="link-here.html" onclick="function(this.href); return flase;">Link Text</a></li>

    Here is a sample of the one of the functions:
    Code:
    function aboutUs_success_handler(o) {
    document.getElementById('ajaxContent').innerHTML = o.responseText;
    }
    
    function aboutUs_failure_handler(o) {
    document.getElementById('ajaxContent').innerHTML = 'Server or your connection is dead';
    } 
    function send_aboutUsRequest() {
    
    var aboutUscallback = { 
        success:aboutUs_success_handler, 
        failure:aboutUs_failure_handler,
        timeout: 10000 
    };
    
    YAHOO.util.Connect.asyncRequest('GET', 'themes/directory-sexy.com/includes/getAboutUs.php', aboutUscallback);
    }
    The only part that changes from one function to the next is this: 'themes/directory-sexy.com/includes/getAboutUs.php' (and the naming of the success,failure and callback) depending upon the link clicked. I would like to eliminate the extra functions and shorten the code a bit.

    Thanx!

  2. #2
    Join Date
    Dec 2008
    Posts
    488
    I'm pretty sure that's the best way to do what your application needs to do. When you need to call functions using success and failure, you have to keep them separate, because the response text is passed automatically. However, if you wanted to do this in another situation, where you're passing all the arguments yourself, you would simply add another argument for the id:
    Code:
    function change_some_text(id,txt){
        document.getElementById(id).innerHTML=txt;
    }
    and call the function like this:
    Code:
    change_some_text('myElement','This is the text');
    However, in this case, the functions need to be left alone, since you cannot pass arguments.

  3. #3
    Join Date
    Dec 2008
    Posts
    488
    Oh I see what you're saying... Missed that, sorry
    Simply use the URL as an argument in the function:
    Code:
    function send_aboutUsRequest(url) {
    
    var aboutUscallback = { 
        success:aboutUs_success_handler, 
        failure:aboutUs_failure_handler,
        timeout: 10000 
    };
    
    YAHOO.util.Connect.asyncRequest('GET', url, aboutUscallback);
    }
    And call the function with:
    HTML Code:
    <a href="javascript: void(0)" onclick="send_aboutUsRequest('themes/directory-sexy.com/includes/getAboutUs.php')">About Us</a>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles