简体   繁体   English

为什么浏览器在onclick的值中使用任何其他单词代替'javascript'时不会抛出错误?

[英]Why don't browsers throw an error when any other word is used in place of 'javascript' in the value of onclick?

I inherited a website, and just came across this curiosity: 我继承了一个网站,只是遇到了这种好奇心:

<a href="/delete"  onClick="jamoscript:return confirm('Do you really want to do that?');">Delete all</a>

I can display the page containing it and click the link to get the confirmation dialog box exactly the same as I do when I change " jamoscript " to " javascript ". 我可以显示包含它的页面,然后单击链接以获得与我将“ jamoscript ”更改为“ javascript ”时完全相同的确认对话框。 No diagnostics are displayed in the Firebug console, either when the page is loaded or when the link is clicked. 在加载页面或单击链接时,Firebug控制台中不会显示任何诊断信息。 What the hey? 嘿嘿? Googling for jamoscript doesn't turn up anything interesting. 谷歌搜索jamoscript并没有发现任何有趣的东西。

Can anybody explain this behavior? 任何人都可以解释这种行为吗?

The string value of an "onclick" attribute is taken to be simple JavaScript code. “onclick”属性的字符串值被视为简单的JavaScript代码。 JavaScript includes provisions for labeled statements , so that code is a return statement with the label "jamoscript". JavaScript包含标记语句的规定,因此代码是带有标签“jamoscript”的return语句。 In other words, this: 换句话说,这个:

jamoscript: return confirm("Do you really want to hurt me?");

is perfectly legal JavaScript. 是完全合法的JavaScript。 Labels aren't used much, so they're unfamiliar. 标签使用不多,所以他们不熟悉。

The only context in which the "javascript:" prefix matters is when a URL is expected, as is the case with the "href" attribute of <a> tags. “javascript:”前缀很重要的唯一上下文是当需要URL时,就像<a>标签的“href”属性一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不工作? 点击javascript - Why don't worked ? Onclick javascript 为什么当其他浏览器没有时,Chrome很难在画布上显示大量图像? - Why does chrome struggle to display lots of images on a canvas when the other browsers don't? 为什么“let = 13”不会在 JavaScript 中抛出错误? (Chrome 控制台 + nodejs) - Why "let = 13" don't throw an error in JavaScript? (Chrome console + nodejs) 为什么不抛出错误[JavaScript]? - Why doesn't this throw an error [JavaScript]? 为什么 javascript function 不适用于 onclick 事件 - Why javascript function don't work for onclick event 问:javascript。 为什么不玩“onclick”? - Q. javascript. why don't play 'onclick'? 以下代码在其他浏览器上不起作用 - Following code don't work on other Browsers 为什么 Safari 允许基于滚动的视频按预期工作,而其他浏览器则不允许? - Why does Safari allow a scroll-based video to work as expected, while other browsers don't? 为什么可以在 Chrome 的 devtools 控制台中重新声明“让”? (其他浏览器不允许。) - Why can 'let' be re-declared in the devtools console in Chrome? (Other browsers don’t allow it.) 为什么访问布尔值的属性不会引发错误-javascript - why does accessing a property of a boolean value not throw an error - javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM