为什么浏览器不抛出一个错误时,任何其他字是用来代替’javascript’在 onclick 的值?

我继承了一个网站,然后发现了这个好奇心:

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

我可以显示包含它的页面,并点击链接获得确认对话框完全一样,当我改变“ jamoscript”为“ javascript”。在加载页面或单击链接时,Firebug 控制台中不显示任何诊断信息。搞什么?搜索 jamoscript 没有任何有趣的发现。

有人能解释一下这种行为吗?

3104 次浏览

The string value of an "onclick" attribute is taken to be simple JavaScript code. JavaScript includes provisions for labeled statements, so that code is a return statement with the label "jamoscript". In other words, this:

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

is perfectly legal 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.