DEV Community

Dirk Levinus Nicolaas
Dirk Levinus Nicolaas

Posted on

Many roads lead to Rome

Become a programming, besides curiosity, also must have intelligence/skills outsmart (if it fails in a certain way, must try in other ways and so on)

The use of a simple editor will be more useful in the process of learning javascript programming, because we are required to explore the entire flow of programs that we make without any automatic correction. If I am asked to recommend, I recommend as I currently use (Notepad++).

Finding references is very necessary for someone who wants to be a programmer, as for the basic references we have to learn that I can recommend are MDN (, W3School ( and Stackoverflow ( . We don't need to learn everything because if that's what we do, we can go crazy alone. We simply search for what we need. Every piece of information we have to document properly for offline reference.

Okay for beginners, let's try:

The challenge is to fill a piece of html in elements that are in HTML with a piece of javascript.
For example we want to put < div >Hello< /div > anywhere in the html, or call the js file or css file to place it in our html.

  <link href="css/cvd.css" rel="stylesheet" />
  <link href="css/mn.css" rel="stylesheet" />
  <script src="js/cmn.js"></script>
  <script src="js/cvs.js"></script>
  <script src="js/mn.js"></script>
  <div id='test'>
  function include(htmlcontent, toEl){
    //because of the .createRange() factor, then htmlcontent in the form of an array
    //used include(['contentHTM', ...], 'html' || 'body' || head /* default = 'head' */)
    toEl = toEl||"head";
    var range = document.createRange();
    var docElement = (toEl == "html"||toEl == "head"||toEl == "body")? docElement = document.getElementsByTagName(toEl)[0] : docElement = toEl
    if(htmlcontent == null){
      alert(new TypeError('The HtmlContent must be filled in'))
    if (htmlcontent.length == 1 ){
    if (htmlcontent.length >1 ){
    /*simplified to be*/
    switch (true){
       case htmlcontent == null : alert(new TypeError('The HtmlContent must be filled in'));break;
       case htmlcontent.length == 1 : docElement.appendChild(range.createContextualFragment(htmlcontent[0]));break;
       case htmlcontent.length >1 :docElement.appendChild(range.createContextualFragment(htmlcontent));;break
  /* Examples of its use*/
  //window.onload = (function(){

    /* Examples of calling js or css files */
    var cvsCss = "css/cvd.css",
        mnCss  = "css/mn.css",
        cmJs  = "js/cmn.js",
        cvsJs = "js/cvs.js",
        mnJs = "js/mn.js",
        stySht = "stylesheet";
    var tpl_css = function(cont){return `<link href=${cont} rel=${stySht} \/>`} 
    var tpl_scr = function(cont){return `<script src=${cont}><\/script>`} 
    // or

    /*Example of filling html in element*/
    include(['<div id=\'test\'></div>'],'body');
  //or none


further development to be more flexible for users, namely appendChild can be selected as appendChid or insertBefore.

That is one illustration, that only with pure javascript, can we create a versatile function.


  1. Being a programmer, we must have curiosity.
  2. We must have the intelligence to outsmart, because there is the term "Many roads lead to Rome".

Top comments (1)

dirkncl profile image
Dirk Levinus Nicolaas