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

JavaScript reserved word: "preset" - Stack Overflow

programmeradmin5浏览0评论

I have a form with a select dropdown, and my select tag looks like this:

<select name='preset' onchange='preset(this);'>

Right now I have my JavaScript function just do alert('test');. Well, when I change my selection in the dropdown, I'm getting an error saying "preset is not a function". Yes, I verified that it's spelled right, and I even did a generic call to it on page load and got my alert.

If I change my function name to something else, like presetx it works just fine. So I thought maybe "preset" was some kind of reserved word in JavaScript, but I can't seem to find anything saying as such. Why would this happen?

Update

Currently I don't have anything else on my test page except for my form and the function. No framework includes or other code, so I know it's not anything like that.

I have a form with a select dropdown, and my select tag looks like this:

<select name='preset' onchange='preset(this);'>

Right now I have my JavaScript function just do alert('test');. Well, when I change my selection in the dropdown, I'm getting an error saying "preset is not a function". Yes, I verified that it's spelled right, and I even did a generic call to it on page load and got my alert.

If I change my function name to something else, like presetx it works just fine. So I thought maybe "preset" was some kind of reserved word in JavaScript, but I can't seem to find anything saying as such. Why would this happen?

Update

Currently I don't have anything else on my test page except for my form and the function. No framework includes or other code, so I know it's not anything like that.

Share Improve this question edited Jul 3, 2011 at 17:27 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jul 3, 2011 at 16:02 newbiejspersonnewbiejsperson 1234 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

Some browsers map elements with name attributes to global variables. So <select name='preset' onchange='preset(this);'> actually creates (in some browsers) a global property preset. This overwrites the preset function.

Since preset is now an HTMLSelectElement object, not a function, you get a "not a function" error.

发布评论

评论列表(0)

  1. 暂无评论