什么时候需要转义JavaScript小书签? [英] When is it necessary to escape a JavaScript bookmarklet?

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

问题描述

即使没有逃脱,我制作的书签似乎也总是可以正常工作,所以为什么要像

Bookmarklets I've made seem to always work well even though I don't escape them, so why do bookmarklet builders like John Gruber's escape special characters like space to %20?

推荐答案

如果使用浏览器的书签属性编辑器将代码复制/粘贴到书签中,则除了换行符和百分号外,不需要编码.但是,事实上,Firefox(也许还有其他浏览器)会为您编码空格.如果在保存后再次检查属性,则会看到它们已编码.您需要对换行进行编码,因为书签只能是1行.您需要对百分号进行编码,因为浏览器希望百分号的视线始终代表编码集中的第一个字符.如果将小书签放在网站上A标签的HREF属性中,则将应用相同的规则,但是还必须对所有双引号进行HTML编码或URL编码.

If you copy/paste code into a bookmark using the browsers bookmark properties editor, you don't need to encode anything except newlines and percent signs. But, in fact, Firefox (and maybe other browsers) will encode spaces for you. If you check the properties again after saving, you'll see them encoded. You need to encode newlines because a bookmark can be only 1 line. You need to encode percent signs because the browser expects percent sights to always represent the first character in an encoded set. If you put the bookmarklet on a website in the HREF property of an A tag, the same rules apply, but you must also either HTML encode or URL encode any double quotes.

许多小书签制造商将只使用encodeURIComponent,它编码的内容比严格要求的要多,但它可能被认为更正确".

A lot of bookmarklet makers will just use encodeURIComponent, which encodes more than what is strictly necessary, but it might be considered more "correct".

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

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