www.webdeveloper.com
Results 1 to 4 of 4

Thread: (simple) return in if statement?

  1. #1
    Join Date
    Mar 2012
    Location
    Ghent
    Posts
    22

    (simple) return in if statement?

    Code:
     	if(customer== ""){
    		document.getElementById('customerInfo').innerHTML = "";
    		return;
    	}


    In this code, Why is the RETURN there? It doesn't work without the return, and it doesn't matter if i put return false, or return true.. (full code underneath)

    Code:
    function toonKlant(klantnaam){
     	if(klantnaam == ""){
    		document.getElementById('hint').innerHTML = "";
    		return;
    	}
    	
    	if(window.XMLHttpRequest){
    		xmlhttp = new XMLHttpRequest();
    	} else {
    		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    	}
    	
    	xmlhttp.onreadystatechange = function(){
    		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
    			document.getElementById("hint").innerHTML = xmlhttp.responseText;
    		}
    	}
    	xmlhttp.open('GET','toonklant.php?name=', true);
    	xmlhttp.send();
    }
    interactive and development tutorials | http://blog.notflip.be

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by notflip View Post
    Code:
         if(customer== ""){
            document.getElementById('customerInfo').innerHTML = "";
            return;
        }


    In this code, Why is the RETURN there? It doesn't work without the return, and it doesn't matter if i put return false, or return true
    It's just a cheap and unclear way of aborting the function if the required data hasn't been passed.

    Having said that, this code seems to be making a request without appending the required data:
    Code:
    
        xmlhttp.open('GET','toonklant.php?name=', true);
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Mar 2012
    Location
    Ghent
    Posts
    22
    I copy pasted before the code was ready =) thanks for the response..

    what would be a good way to abort the function if false? thanks!
    interactive and development tutorials | http://blog.notflip.be

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by notflip View Post
    what would be a good way to abort the function if false? thanks!
    In this case, don't allow the function to be called without a parameter.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

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