这是“数据:”URI,在浏览器中加载它。
data:text/html,<script>alert("#")</script>
我在 Chrome 中执行了 alert(),但在 Firefox 中没有。 Firefox 删除“#”字符和所有后续字符。 我怎样才能让 FF 发出 alert("#")?
更新:我理解“#”片段部分,但问题更像是“为什么 Chrome 会忽略“片段”大小写并将其视为普通字符,而 FF 却没有?”。
最佳答案
数据 URI 的数据部分必须进行编码,#
不允许作为文字字符。来自Wikipedia page
The data, separated from the preceding part by a comma (
,
). The data is a sequence of octets represented as characters. Permitted characters within a data URI are the ASCII characters for the lowercase and uppercase letters of the modern English alphabet, and the Arabic numerals. Octets represented by any other character must be percent-encoded, as in%26
for an ampersand (&
).
...引用 RFC3986 .
所以你的数据 URI 应该是:
data:text/html,%3Cscript%3Ealert(%22%23%22)%3C%2Fscript%3E
...同时适用于 Chrome 和 Firefox:
<a href="data:text/html,%3Cscript%3Ealert(%22%23%22)%3C%2Fscript%3E">Click here</a>
您可以使用 JavaScript 的 encodeURIComponent
获取 URI 数据,例如:
var dataUri = "data:text/html," + encodeURIComponent('<script>alert("#")</script>');
关于javascript - Chrome 与 Firefox Javascript 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41464385/