SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    callback function > return string

    I would like to get the word 'test' from the callback function psCallback below.

    alert(opt); is now displaying the word 'test'

    What am I doing wrong?

    PHP Code:

    function psCallback() {
        var 
    content 'test';
        
        return 
    content;
    }
        
        
    jQuery(document).ready(function () {
        
        var 
    opt = {callbackpsCallback};
            
        
    alert(opt);    
            
        return 
    false;

    }); 

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,003
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    A couple of pointers:

    You don't need jQuery to do this.
    You don't need to wrap your code in a call to $(document).ready().

    That said, the reason why you aren't seeing the results you expect is that opt is an object literal and you are just alerting its string representation to the screen.
    You can inspect what is happening a little better if you write console.log(opt);, as the console will format things a little nicer for you.

    What you need to do is to alert the object's callback attribute, which is a pointer to your function.

    e.g.

    Code JavaScript:
    function psCallback() { 
      var content = 'test'; 
      return content; 
    } 
     
    var opt = {callback: psCallback}; 
    alert(opt.callback());

    Hope that helps.


Bookmarks

Posting Permissions

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