简体   繁体   English

JavaScript HTML注入效率/最佳实践

[英]JavaScript HTML injection efficiency/best practice

I'm looking to inject HTML via JavaScript into a page at work. 我正在寻找通过JavaScript将HTML注入工作页面的方法。

What I'd like to know is if injecting a re-write of the page is more or less efficient than injecting snippets throughout the page with methods like getElementById() . 我想知道的是,与使用getElementById()类的方法在整个页面中插入摘要相比,注入页面重写是否更有效或更有效。

For example: 例如:

document.getElementById("Example").innerHTML = '<h2 id="Example" name="Example">Text</H2>'
document.getElementsByClassName("Example").innerHTML = '<H1>Test</H1>'

...etc. ...等等。 Is this more efficient/effective than simply injecting my own version of the entire page's HTML start to finish? 这比简单地注入我自己的整个页面HTML版本更有效/有效吗?

Edit: Per Lix's comment, I should clarify that I likely will be injecting a large amount of content into the page, but it will affect no more than a dozen elements at any time. 编辑:根据Lix的评论,我应该澄清一下,我很可能会将大量内容注入到页面中,但是在任何时候都不会影响十几个元素。

If your project can manage it, it could be better to create DOM Elements and append them to the tree . 如果您的项目可以管理它,那么创建DOM Elements并将它们附加到tree可能更好。

The big problem with efficiency would be that setting .innerHTML property would first remove all the nodes and only then parse the html and append it to the DOM. 效率的大问题是设置.innerHTML属性将首先删除所有节点,然后再解析html并将其附加到DOM。

It's obvious that you should avoid removing and the re-appending identical elements, so if you're sure the "Example" elements would always remain on the page, your way of setting them seems to be a nice optimazation. 显然,您应该避免删除和重新添加相同的元素,因此,如果您确定"Example"元素始终保留在页面上,则设置它们的方式似乎是一个不错的优化。

If you want to optimize it even further, you could parse the html you want to append to nodes and have a function that checks which ones should be appended and which one shouldn't. 如果要进一步优化它,可以解析要附加到节点的html,并具有一个功能,该功能可以检查应该附加哪些,不应该附加哪些。 But be aware that accessing the DOM is costly. 但是请注意,访问DOM的成本很高。 Read more about the ECMA-DOM bridge . 阅读有关ECMA-DOM桥的更多信息。

Edit: In some cases it might be better to let the browser do the html parsing and injecting through innerHTML . 编辑:在某些情况下,最好让浏览器通过innerHTML进行html解析和注入。 It depends on the amount of HTML you're inserting and the amount you're deleting. 这取决于您要插入的HTML的数量和要删除的HTML的数量。 See @Nelson Menezes 's comments about innerHTML vs. append. 参见@Nelson Menezes关于innerHTML与append的评论。

Depends on the context. 取决于上下文。 If it was only decoration of existing content, then your proposal would suffice. 如果仅是对现有内容的修饰,那么您的建议就足够了。 I'd use jQuery anyway, but that's only my preference. 无论如何,我都会使用jQuery,但这只是我的偏爱。

But when injecting the actual content you have two concerns: 但是,在注入实际内容时,您有两个问题:

  • maintainability - Make the structure of your code readable and subject to easy change when you need (and you will need). 可维护性-使代码的结构易读,并在需要时(并且将需要)易于更改。
  • accessibility - When javascript is disabled, then no content will be visible at all. 可访问性-禁用javascript时,所有内容都将不可见。 You should provide a link to desired content in <noscript/> tag or ensure accessibility to everyone any other way you prefer. 您应该在<noscript/>标记中提供所需内容的链接,或确保以任何其他您喜欢的方式对所有人进行访问。 That's a minority of internet users at the moment, but for professional webmasters they make it count. 目前,这是少数互联网用户,但对于专业的网站管理员来说,这很重要。

To address both of above concerns I prefer to use ajax to load a whole page, some part or even plaintext into existing element. 为了解决以上两个问题,我更喜欢使用Ajax将整个页面,部分甚至纯文本加载到现有元素中。 It makes it readable, 'cause the content is sitting in another file completely separated from the script. 它使内容易于阅读,因为内容位于与脚本完全分开的另一个文件中。 And since it's a file, you may redirect to it directly when javascript is disabled. 并且由于是文件,因此您可以在禁用javascript时直接将其重定向到该文件。 It makes the content accessible to anyone. 它使任何人都可以访问内容。

For plain javascript you'd have to use XMLHttpRequest object, like here . 对于普通的javascript,您必须使用XMLHttpRequest对象,例如here With jQuery it's even simpler. 使用jQuery,它甚至更简单。 Depending on what you need you may use .load , .get or .ajax . 根据您的需要,您可以使用.load.get.ajax

Best practice today is using JQuery Manipulation functions . 今天的最佳实践是使用JQuery Manipulation函数

Most time you'd use one of this 3 functions : 大多数时候,您会使用以下三个功能之一:

  1. Replace existing HTML node: 替换现有的HTML节点:

    $("div").html("New content"); $(“ div”)。html(“新内容”);

  2. Append a sibling node: 追加同级节点:

    $("div").append("New content"); $(“ div”)。append(“ New content”);

  3. Remove a node: 删除节点:

    $("div").remove(); $(“ div”)。remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM