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

冷暖自知 提交于 2019-12-18 15:35:16

问题


I was creating a form validator for a client and ran into this weird error ONLY in Internet Exploder (Explorer) 7/8....

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

Of course, there was no code whasoever 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 miss-direct.

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, any returns actually outside of a legitimate function definition.

Has anybody else had any instances when this has happened to you?


回答1:


I got it!

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!

Hope this saves someone else from the hell I went through.




回答2:


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 its 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?




回答3:


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.




回答4:


I was getting this error with a ModalPopupExtender. I had specified the following attribute: OnOkScript="return false;"

Setting OkOkScript="" solve this issue for me.



来源:https://stackoverflow.com/questions/3168713/i-get-this-error-return-statement-outside-of-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!