SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast Red Blaze's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to understand javascript

    I want to know what "return;" is for. What does it do?

    Reason is, it's because I have a big block of code, and when it reaches an if statement and it's true, I want it to stop there.

    Code:
    function fav(what, action, xmlnum, id){
    var xmlhttp=false; //Clear our fetching variable
            try {
                    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x objectů
            } catch (e) {
                    try {
                            xmlhttp = new
                            ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
                } catch (E) {
                    xmlhttp = false;
                            }
            }
            if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                    xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
            }
    			if(what == 'art'){
    			var file = 'fav.php?what='+what+'&action='; //This is the path to the file we just finished making *
        xmlhttp.open('GET', file + action + '&xmlnum=' + xmlnum + '&artid=' + id, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    			}
    			if(what == 'artist'){
    			var file = 'fav.php?what='+what+'&action='; //This is the path to the file we just finished making *
        xmlhttp.open('GET', file + action + '&artistid=' + id, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    
    //I want it to stop here if this statement is true
    
    			}
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                    var Details = xmlhttp.responseText; //The content data which has been retrieved ***
                    if( Details ){ //Make sure there is something in the content variable
                          document.getElementById('Details').innerHTML = Details; //Change the inner content of your div to the newly retrieved content ****
                    }
            }
            }
            xmlhttp.send(null) //Nullify the XMLHttpRequest
    return;	
    }
    Is there a way around this? I know that in PHP, I can use the exit;. Can it be used for javascript as well? Thanks for the help!

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "return" does exactly what it implies, it returns from the function. It is similar to exit but not quite the same thing. Exit will stop all execution of the running script. Return will stop execution of the running function but the application will still continue. Return can also have an value with it. Javascript doesn't require function type declarations, but you can return an integer value or boolean value, or really any object.

    Example:


    Code:
     
     // this is a function that returns nothing
     function doSomething(value) {
     
       if (value == 5) {
     	 alert('5 was entered');
     	 return;
       }
       // do some other stuff with value ??
     }
     
     // this is the same thing
     function doSomething(value) {
       if (value == 5) {
      	 alert('5 was entered');
       } else {
     	 // do some other stuff with value ??
       }
     
     }
     
     // integer return
     function add(x,y) {
        return (x+y);
     }


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
  •