Skripty dokumentů HTML DOM
Příklady
Počet prvků <script> v dokumentu:
document.scripts.length;
Vraťte obsah prvního prvku <script>:
document.scripts[0].text;
Vraťte obsah prvního prvku <script>:
document.scripts.item(0).text;
Další příklady níže.
Definice a použití
Vlastnost scripts
vrací kolekci všech prvků <script> v dokumentu.
Vlastnost scripts
vrací HTMLCollection .
Vlastnost scripts
je pouze pro čtení.
Viz také:
HTML kolekce
HTMLCollection je kolekce uzlů HTML.
K uzlům v kolekci lze přistupovat pomocí čísel indexů. Index začíná na 0.
Vlastnost length vrátí počet prvků v kolekci.
Syntax
document.scripts
Vlastnosti
Vlastnictví | Popis |
délka | Počet prvků <script> v kolekci. |
Metody
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
Návratová hodnota
Typ | Popis |
Objekt | Objekt HTMLCollection. Všechny prvky <script> v dokumentu. Prvky jsou seřazeny tak, jak se objevují v dokumentu. |
Další příklady
Vraťte obsah prvku <script> s id="myScript":
document.scripts.namedItem("myScript").text;
Opakujte všechny prvky <script> a vypište každé ID:
const collection = document.scripts;
let text = "";
for (let i = 0; i < collection.length; i++) {
text += collection[i].id + "<br>";
}
Podpora prohlížeče
document.scripts
je funkce DOM úrovně 2 (2001).
Je plně podporován ve všech prohlížečích:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |