Add script content to page on runtime - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Script

Description

Add script content to page on runtime

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from   w w  w .  ja  va  2 s .  co m*/
var script = document.createElement('script');
script.text = "var a = 1;console.log(a);" ;
//to use external js then use src
document.getElementsByTagName('head')[0].appendChild(script);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials