Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason is two-fold:</p> <ol> <li><p>Accessing anything by tag name (document.getElementsByTagName) is ALWAYS going to be slower than accessing something by id (document.getElementById). This is because getElementByTagName always returns a LIVE nodeList (or HTMLCollection). This means not only does it have to search through the entire document structure to find all the titles (and return an collection of titles), it will keep the list live and updated incase of insertion. Whereas, most browsers have a very fast method of indexing ids and it will return only the element reference (i.e. will not have to keep anything updated).</p></li> <li><p>If you have frames on the page, then document.getElementsByTagName('title') will return a list of all the titles on the page (i.e. including any iframe titles), which is clearly not what they want. They would then have to search through the nodeList to find the title they are looking for. I did a quick example on jsfiddle: <a href="http://jsfiddle.net/9QtZu/" rel="nofollow">http://jsfiddle.net/9QtZu/</a>:</p> <pre><code>var titles = document.getElementsByTagName('title'); console.log(titles); </code></pre></li> </ol> <p>So, using document.getElementById allows them to more quickly access main title that they are after, whilst also boosting performance. No contest.</p> <p><a href="https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById</a> <a href="https://developer.mozilla.org/en-US/docs/Web/API/element.getElementsByTagName" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/API/element.getElementsByTagName</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload