最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return not in function - Stack Overflow

programmeradmin7浏览0评论

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.

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.

Share Improve this question edited Apr 27, 2009 at 22:17 dougfelt asked Apr 27, 2009 at 21:22 dougfeltdougfelt 3352 gold badges3 silver badges9 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 6

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.

Javascript returns an error because the return statement is not inside a function. A possible cause for this is incorrect function definition, such as:

myFunc() { some code; return; }

where the correct code definition is:

function myFunc() { some code; return; }

I'm going to hazard a guess that this means you have an extra closing brace, or a missing opening brace.

Is your codebase prohibitively large to do a spot check around each of your return functions? Do you have an IDE that highlights matching braces for you?

I got this when typing into the console window in firebug. I'd written a for loop to search for something in a list dont try and return a value in there - just assign it to a variable and you can see the object/value in the console.

Daniel Lew has the answer for dougfelt, but others who get this error should also look out for any return statement that doesn't reside in a function. For instance:

<script type="text/javascript">
    alert("I'm JavaScript!");
    return false;
</script>

return false; is outside of any function, and will cause this error.

发布评论

评论列表(0)

  1. 暂无评论