Javascript Element How to - Get script element content








Question

We would like to know how to get script element content.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from w  w w  .  j  ava2s .com-->
    console.log(document.getElementById('code1').innerHTML);
});
</script>
</head>
<body>
  <script id="code1" type="text/myjs">alert('hi')</script>
</body>
</html>

The code above is rendered as follows: