Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return not in function

Firebug is reporting a "return not in function" error with no location (well, line 1 of nothing). How can I track down the source of this error?

return not in function
[Break on this error] return(0)
javascript:return... (line 1)

I'm running FireBug 1.05 on FF 2.0.0.20 on Ubuntu.

I found a solution that works (for this configuration):

  var link = document.createElement('a');
  link.href='/';
  if (childSummary.more) {
    link.onclick = capture(function(id) { follow(id); }, childSummary.id);
  } else {
    link.onclick = capture(function(id) { show(id); }, childSummary.id);
  }
  link.appendChild(document.createTextNode(name));
  div.appendChild(link);

  [...]

 function capture(fn, val) {
   return function() { fn(val); return false; };
 }

The code was in a loop in which the id was changing, necessitating the capture function.

Formerly the href was 'javascript: return 0' and the capture function wasn't returning false directly, instead using the result of the fn, and there was a path when it was returning the equivalent of true. The href was being evaluated causing the error.

Defining href as '#' or '' caused all the links to appear as already visited. Not defining href at all caused there to be no link highlighting. This seemed simplest.

like image 409
dougfelt Avatar asked Apr 27 '09 21:04

dougfelt


People also ask

Why is my return outside of the function?

This syntax error is nothing but a simple indentation error, generally, this error occurs when the indent or return function does not match or align to the indent of the defined function.

Can you use return outside a function JavaScript?

The return and yield statements must be in a function, because they end (or pause and resume) function execution and specify a value to be returned to the function caller.

Can a function be returned?

A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.

How do you solve return can be used only within a function?

To solve this error, make sure all of your return statements are properly indented and appear inside a function instead of after a function.


1 Answers

I think the "javascript:return ..." is telling. I believe you're trying to return a value in the href attribute of an anchor, as below:

<a href="javascript: return false">Test</a>

The reason Firebug isn't telling you the location is because it's not in any JavaScript, but is rather in a one-liner in the DOM.

like image 90
Dan Lew Avatar answered Oct 20 '22 19:10

Dan Lew