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

javascript - YES or NO to semicolon in object events - Stack Overflow

programmeradmin0浏览0评论

I've searched, but didn't find answer to this question. Should HTML DOM EVENTS, like onChange, onSelect, onKeyUp, onFocus, onClick etc. contain semicolon, example two lines below.

onChange="this.form.submit();" OR onChange="this.form.submit()"

"YES" or "NO" or "Doesn't Matter"

I guess it doesn't matter, but again, what's the best, most right to do?

I've searched, but didn't find answer to this question. Should HTML DOM EVENTS, like onChange, onSelect, onKeyUp, onFocus, onClick etc. contain semicolon, example two lines below.

onChange="this.form.submit();" OR onChange="this.form.submit()"

"YES" or "NO" or "Doesn't Matter"

I guess it doesn't matter, but again, what's the best, most right to do?

Share Improve this question edited Feb 8, 2023 at 20:27 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 23, 2012 at 23:06 ProDrazProDraz 1,2816 gold badges24 silver badges43 bronze badges 3
  • 6 inline event-handlers are so 90s. – canon Commented Aug 23, 2012 at 23:08
  • @canon That's not the case, I just want to clarify that ";" DILEMMA to my self and few other colegues :P – ProDraz Commented Aug 23, 2012 at 23:10
  • 1 And onchange, onselect etc. aren't events, they are event handlers. – NullUserException Commented Aug 23, 2012 at 23:10
Add a ment  | 

4 Answers 4

Reset to default 9

It doesn't matter.

The event handler attribute value is treated as a series of statements which are wrapped in a function signature similar to

 function (event) {
   with (event.target.ownerDocument) {
     with (event.target) {
       // attribute body goes here
     }
   }
 }

so you can put any group of SourceElements in the attribute value, and can leave off semicolons as per JavaScript's usual semicolon insertion rules.

Never used it with semicolon, so I guess it doesn't matter.

No, you do not need a semicolon. I do not know if it would work with it but without is fine. If you need to know anything about web languages like HTML, CSS, or Javascript see www.w3school. for reference or tutorials. The link to the Javascript section is: http://www.w3schools./js/default.asp A link to an example of what you were asking for is: http://www.w3schools./js/js_popup.asp This example uses the onclick you mentioned in your question.

It's really a matter of style; as with any JavaScript code snippet, you are allowed to omit unambiguous semicolons if you want to. (I prefer to put them in.)

发布评论

评论列表(0)

  1. 暂无评论