Have javascript generate html elements/contents next to its defined js source file <script> tag

The following javascript codes will generate "Hello world!" content wrapped with <p> <div> tags next to where the client-side javascript source file <script> tag is. Have these javascript codes in external js file let say "hello.js":

  
var script = document.getElementsByTagName('script');
var newdiv = document.createElement('div');
script = script[script.length - 1];
form.innerHTML = '<p>Hello world!</p>';
script.parentNode.insertBefore(newdiv, script.nextSibling);
  

... and define this "hello.js" file anywhere you want the content to appear inside your <body> tag.

  
<script src="hello.js" type="text/javascript"></script>
  

Next to where you defined the js source file, its should generate the following output:

  
<div><p>Hello world!</p></div>
  
Tags

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.