Jump to content

Recommended Posts

I was getting warnings in my error console saying that my "functions may not return a value." I then went through them all making sure they did return a value, so i put in return true; in all the functions that didn't have a return value. It worked fine in FF2, but now that FF3 is out, they no longer work. If I remove return true; then it works fine.

 

So to remove the warning, what should I return?

Link to comment
https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/
Share on other sites

A warning is just that - a warning. It is good practice to always explicitly do a return in my opinion. But, if you have a function that does not need to return anything there is no reason you HAVE to have it return something. I think the warning is there in case you have a function that is supposed to return something there may be a specific branch of th elogic that doesn't - that would be a problem that needs to be fixed.

 

I have not done any testing in FF3, but would be interested to see a specific function that does not work by adding a return statement.

Here is one of my functions that doesn't return anything:

function friendConfirmedSearchPage(id){
var ajaxRequest;
try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			alert("Your Browser Doesn't support AJAX.");
			return false;
		}
	}
}
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		document.getElementById('friendValueStatus_'+id).value = ajaxRequest.responseText;
		var frendRequest = document.getElementById('friendValueStatus_'+id).value;
		if(frendRequest=='true'){
			document.getElementById('friendRequestStatus_'+id).innerHTML = '<strong>Friend Pending</strong>';
		}else{
			document.getElementById('friendRequestStatus_'+id).innerHTML = '<strong>Friend Failed</strong>';
		}
	}
}
ajaxRequest.open("GET", '/process/becomeFriends?id='+id, true);
ajaxRequest.send(null);
}

 

if I add return true; to the end of that function, it will take me to a page that says "true"

How are you calling the function? If the try fails would it output a page that just says "false?" If you aren't checking if the function is true or false when you call it then you don't really need it to return false. If that is the case, try just using return; in place of both return true and false.

For ajax functions that are connected to <a> tags, you have to return false, or the browser will follow the link after executing the javascript.

 

Not just for AJAX functions, but for any JavaScript function embedded in a link.  'return false' tells the browser to not perform the default action which, in this case, is to follow the hyperlink's href property.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.