Append javascript script to head - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:append

Description

Append javascript script to head

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.js"></script> 
 </head> /*from   ww w . ja  va2 s  . co  m*/
 <body> 
  <a href="javascript:void(0)">CLICK</a> 
  <script type="text/javascript">
$(document).ready(function () {
    $('a').on('click', function () {
        $('<script>').text('test()').appendTo('head');
     });
});
function test(){
   console.log("running")
}

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

Related Tutorials