The Little Guy Posted June 25, 2008 Share Posted June 25, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/ Share on other sites More sharing options...
Psycho Posted June 25, 2008 Share Posted June 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-574225 Share on other sites More sharing options...
The Little Guy Posted June 25, 2008 Author Share Posted June 25, 2008 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" Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-574298 Share on other sites More sharing options...
Psycho Posted June 25, 2008 Share Posted June 25, 2008 Interesting. Are you putting the return true at the very end of the friendConfirmedSearchPage() function or at the end of the onreadystatechange function? Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-574344 Share on other sites More sharing options...
The Little Guy Posted June 25, 2008 Author Share Posted June 25, 2008 right after ajaxRequest.send(null); Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-574350 Share on other sites More sharing options...
lemmin Posted June 25, 2008 Share Posted June 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-574363 Share on other sites More sharing options...
The Little Guy Posted June 27, 2008 Author Share Posted June 27, 2008 I am using it like this: <a href="javascript:friendConfirmedSearchPage(43)">link</a> Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-575960 Share on other sites More sharing options...
haku Posted June 27, 2008 Share Posted June 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-575996 Share on other sites More sharing options...
KevinM1 Posted June 27, 2008 Share Posted June 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111866-warning-does-not-always-return-a-value/#findComment-576151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.