Document.scripts

  • Revision slug: Web/API/document.scripts
  • Revision title: Document.scripts
  • Revision id: 412673
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{DomRef}}

Summary

Returns a list of the {{HTMLElement("script")}} elements in the document. The returned object is an {{domxref("HTMLCollection")}}.

Syntax

var scriptList = document.scripts;

The returned scriptList is an {{domxref("HTMLCollection")}}. You can use this just like an array to get all the elements in the list.

Example

This example looks to see if the page has any {{HTMLElement("script")}} elements.

var scripts = document.scripts;

if (scripts.length) {
  alert("This page has scripts!");
}

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop("9.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile("9.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

Specification

  • {{spec("http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-scripts", "DOM: document scripts")}}

Revision Source

<div>{{DomRef}}</div>


<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a list of the {{HTMLElement("script")}} elements in the document. The returned object is an {{domxref("HTMLCollection")}}.</p>


<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>var <em>scriptList</em></code> = document.scripts;
</pre>
<p>The returned <code>scriptList</code> is an {{domxref("HTMLCollection")}}. You can use this just like an array to get all the elements in the list.</p>


<h2 id="Example">Example</h2>
<p>This example looks to see if the page has any {{HTMLElement("script")}} elements.</p>


<pre class="brush:ja">
var scripts = document.scripts;

if (scripts.length) {
  alert("This page has scripts!");
}
</pre>


<h2 id="Specification" name="Specification">Browser compatibility</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoDesktop("9.0")}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoMobile("9.0")}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>


<h2 id="Specification" name="Specification">Specification</h2>
<ul>
<li>{{spec("http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-scripts", "DOM: document scripts")}}</li>
</ul>
Revert to this revision