我刚刚在脚本中发现 chrome 中创建的标签 id 存储在 window 对象上。
<script id="deploy" type="text/html">blah</script>
window.deploy
= ➜
<script id="deploy" type="text/html">blah</script>
Object.keys(window)
不包含“deploy”
所以我的问题
有没有人找到解决这个问题的方法(防止 chrome 污染我的世界)这是我唯一想到的 是以下内容:-
$('script[type="text/html"]').each ->
# stuff
delete window[@.getAttribute 'id']
我的“解决方案”有在不使用 chrome 时删除全局变量的风险。
但是如果没有我的解决方案,chrome 可以在脚本 ID 冲突时覆盖全局变量。
一团糟!
感谢任何帮助!
最佳答案
If you name an element in your HTML document using the
id
attribute, and if the Window object does not already have a property by that name, the Window object is given a nonenumerable property whose name is the value of theid
attribute and whose value is the HTMLElement object that represents that document element.
(由我强调)
关于javascript - 有没有解决chrome中意外全局变量的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213150/