实际上不赞成使用document.write吗? [英] Is document.write actually deprecated?

查看:58
本文介绍了实际上不赞成使用document.write吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经听说过有关不推荐使用document.write的话题,但是我不确定该信息的确切来源. 我确实在MDN中进行了查找,但没有关于它已被弃用的任何符号..所以现在我有点可疑.不幸的是,谷歌也没有太多帮助(也许我只是没有使用正确的搜索词).

I've seen a bit of chatter about document.write being deprecated, but I'm not sure exactly where this information came from. I did look it up in MDN, but there wasn't any notation about it being deprecated.. so now I'm a bit suspicious. Google wasn't much help either unfortunately (perhaps I just wasn't using the right search terms).

如果确实不推荐使用它,有人可以将我链接到表明确实不推荐使用的适当文档吗?

If it is indeed deprecated, can someone link me to the appropriate documents showing that it is indeed deprecated?

推荐答案

否.它只是最常被认为是不良做法,几乎与eval一样被滥用.

No. It's just most often considered bad practice and almost as misused as eval.

阅读:为什么document.write被认为是不良做法"?

引用上面链接的问题中的一些要点:

Quoting some important points from the linked question above:

  • document.write(此后称为DW)在XHTML中不起作用

  • document.write (henceforth DW) does not work in XHTML

在页面加载完成后执行的DW将覆盖该页面,或写入一个新页面,或者不起作用

DW executed after the page has finished loading will overwrite the page, or write a new page, or not work

DW在遇到的地方执行:它无法在给定的节点上注入

DW executes where encountered: it cannot inject at a given node point

正如@JaredFarrish所述,deprecated主要是一种心理状态.这是一件很可能永远不会消失的遗物,否则它将破坏许多站点-甚至

Also as @JaredFarrish stated, deprecated is mostly a state of mind. It's a relic that most likely will never go away otherwise it'd break many sites - even the Traditional Google Analytics code uses DW.

很明显,从功能上讲,它早已被适当的DOM操作方法所取代,并再次引用了上面链接的问题:DW is effectively writing serialised text which is not the way the DOM works conceptually.

Obviously, functionality-wise it has been superseded long ago by proper DOM manipulation methods, and quoting the linked question above again: DW is effectively writing serialised text which is not the way the DOM works conceptually.

为了平衡,以下列出了DW可能被认为合适的地方:

To counterbalance, here's a list of where DW may be considered appropriate:

  • DW本身没有什么问题-如果使用得当,它不会破坏您的页面;
  • 它允许轻松地将外部脚本回退到本地副本,例如从CDN加载jQuery失败时:

  • There's nothing wrong with DW, per se - it won't destroy your page if you use it appropriately;
  • It allows for easily fallbacking an external script to a local copy such as when loading jQuery from a CDN fails:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.8.2.min.js">\x3C/script>');</script>

  • 这是在页面中插入外部摘要同时保持代码简短(例如分析)的最简单方法之一.请注意,例如Google Analytics(分析)之类的系统现在更喜欢异步方法-通过document.createElement API创建script元素,设置其属性并将其附加到DOM-而不是使用其传统的document.write方法.

  • It's one of the easiest ways to insert external snippets in your page while keeping the code short (e.g. analytics). Note that systems such as Google Analytics now favor the asynchronous method - creating a script element through the document.createElement API, setting its properties and appending it to the DOM - rather than using its traditional document.write method.

    tl;博士:
    DW很容易被滥用,因此,在现实世界中,只要可行,就应该使用适当的DOM操作方法.

    tl;dr:
    DW is easily misused, hence for real world use, prefer a proper DOM manipulation method whenever viable.

    这篇关于实际上不赞成使用document.write吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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