SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Make variable global

    How can I make my alert work? Right now it is undefined and I need to make my function in the variable global. Can this be done?

    Code JavaScript:
    if (json_goals === undefined)
    {
    	ajax.get('/assets/ajax/user_goals.php', function (resp)
    	{
    		var json_goals = resp;
     
    		return json_goals;
    	});
    }
    alert(json_goals);

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by unemployment View Post
    How can I make my alert work? Right now it is undefined and I need to make my function in the variable global. Can this be done?

    Code JavaScript:
    if (json_goals === undefined)
    {
    	ajax.get('/assets/ajax/user_goals.php', function (resp)
    	{
    		var json_goals = resp;
     
    		return json_goals;
    	});
    }
    alert(json_goals);
    The callback function runs later on from a global scope, so the return won't do anything useful.

    Try replacing the return with the alert instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •