javascript - 有没有解决chrome中意外全局变量的方法

标签 javascript google-chrome

我刚刚在脚本中发现 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”

Not a new issue apparently

所以我的问题

有没有人找到解决这个问题的方法(防止 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 the id attribute and whose value is the HTMLElement object that represents that document element.

(由我强调)

来源:JavaScript: The Definitive Guide, 6th Edition

关于javascript - 有没有解决chrome中意外全局变量的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213150/

相关文章:

javascript - 如何在单击按钮时显示 Bx-slider

testing - 如何在没有 EULA 的情况下启动 ChromeDriver.exe?

javascript - D3 中荷兰的自定义数据 map

javascript - 从函数内调用 setInterval

google-chrome - Chrome 在非 ssl heroku web 应用程序上强制使用 ssl

internet-explorer - IE 条件注释和 Chrome/Firefox

google-chrome - 更改 Chrome 扩展程序图标

google-chrome - 在Chrome开发者面板中,是否有元素选择器的键盘快捷键?

javascript - 如何在没有 API key 的情况下使用谷歌地图

javascript - 我是否应该将函数从 Controller 移至服务,但为了单元测试而必须使用 rootScope 而不是作用域?