开发者

I get this error! [ 'return' statement outside of function ]

I was creating a form validator for a client and ran into this weird error only in Internet Explorer (Exp开发者_如何学JAVAlorer) 7/8....

'return' outside of function, line 1, char 1

Of course, there was no code whatsoever on line 1. It was a simple commented statement. And there was nothing wring with it in any way. So I knew it was just a debug misdirection.

I have been pulling my hair out to understand what could be wrong here...

I have already ruled out the obvious: return statements in a loop, too many return statements in a single function, and any returns actually outside of a legitimate function definition.

What is the reason for this?


The problem was that I was using a return statement to override the default behavior for my form and it was assigned to a property, but it was not placed inside an anonymous function!

I had a form element that was set up like this:

<form name="formname" onSubmit="javascript:validateForm(this);" action="javascript:return false;" method="post" enctype="multipart/form-data">

All I needed to change was the action property to this:

… action="javascript:function(){return false};"

It now works flawlessly!


Sometimes, when you're writing back end systems where the user is expected to have JavaScript enabled in order to use the system, I feel it's fine to use javascript: href tags, since use of the system requires JavaScript anyway.

In this case, there is nothing wrong with using href="javascript:", is there?


Check to make sure you are not writing "var" in your function parameters.

In many languages you must specify the type of the argument parameters. In JavaScript you do not have to.


I was getting this error with a ModalPopupExtender. I had specified the following attribute:

OnOkScript="return false;"

Setting OkOkScript="" solved this issue for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜