Javascript DOM HTML Script Object get

Introduction

The Script object represents an HTML <script> element.

We can access a <script> element via document.getElementById():

var x = document.getElementById("myScript");

Click the button to get the URL of the external script file.

View in separate window

<!DOCTYPE html>
<html>
<body>
<script id="myScript" src="javascript.js"></script>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//w  w  w  .  j a  va2s.  c  o m
  var x = document.getElementById("myScript").src;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related