我什么时候需要指定JavaScript协议? [英] When do I need to specify the JavaScript protocol?

查看:217
本文介绍了我什么时候需要指定JavaScript协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是,我只需要在URL属性中使用JavaScript时指定协议,例如在hrefs中。这是 javascript的唯一有用上下文:

I was under the impression that I only need to specify the "protocol" when using JavaScript in URL attributes, such as in hrefs. Is this the only "useful" context for javascript:?

明智:

<a href="javascript:alert('Hello')">World!</a>

愚蠢:

<form onsubmit="javascript:alert('oops!')">

这是对的吗?或者是否需要注意一些模糊的bug /用例?

Is this right? Or is there some obscure bug/use case I need to be aware of?

推荐答案

javascript: 事件处理程序上的伪协议将被忽略,您不需要它,JavaScript引擎会将 javascript:解释为标签说明

The javascript: pseudo-protocol on event handlers will be only ignored, you don't need it, the JavaScript engine will interpret javascript: as a Label Statement.

标签只是为一个语句提供一个标识符,并允许你在程序的其他地方引用它。

A label simply provides an identifier to a statement, and lets you refer to it elsewhere in your program.

恕我直言,这个伪协议只对bookmarklet有用...

IMHO, this pseudo-protocol is only useful for bookmarklets...

推荐文章:

  • The useless javascript: pseudo-protocol

这篇关于我什么时候需要指定JavaScript协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆