I master the method by which content (text) is uploaded to a web page from a json file. Content before being placed on the page is compiled with handlebars templates. Javascript files with logic for all of this, written as commonjs modules copied using browserify. I start the page from a local http server installed using NodeJS npm.

The test case worked, when I tried to apply this method to the finished web page, something was wrong. So as soon as I master this method, I will be grateful for the help.

So, there is:

  • webContent.json - it stores the content (text) that will be loaded into index.html
  • contentProcess.js - contains a function that sends an Ajax request to webContent.json , parses, compiles with the Handlebars Template and puts the content on the page (using innerHTML)
  • addContent.js - calls a function from contentProcess.js , substituting the necessary variables. contentProcess.js and addContent.js are written and linked as commonjs modules. Then they are compiled into main.js using browserify .
  • index.html - contains the Handlebars Template , div , where the content and link to main.js will be placed

The test version worked. When I tried to implement this approach with an existing web page, something was not working. The console complains about part of the logic from main.js , which was originally part of the function in contentProcess.js , which worked without problems in the test example (below are two screenshots): enter image description here

enter image description here What is the problem here (did this function work in the test version)?

In snippet scattered code fragments of all the above files (separated by comments):

 /* --- webContent.json --- */ { header: "Powerful business", describtion: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec turpis neque, sodales a faucibus at, viverra luctus urna. Suspendisse dignissim neque dui, in tincidunt arcu.' } /* --- contentProcess.js --- */ module.exports = function(jsonDir, templId, finId){ function sendGet(callback) { /* create an AJAX request using XMLHttpRequest*/ var xhr = new XMLHttpRequest(); /*reference json url taken from: http://www.jsontest.com/*/ /* Specify the type of request by using XMLHttpRequest "open", here 'GET'(argument one) refers to request type "http://date.jsontest.com/" (argument two) refers to JSON file location*/ xhr.open('GET', jsonDir); /*Using onload event handler you can check status of your request*/ xhr.onload = function () { if (xhr.status === 200) { callback(JSON.parse(xhr.responseText)); } else { alert(xhr.statusText); } }; /*Using onerror event handler you can check error state, if your request failed to get the data*/ xhr.onerror = function () { alert("Network Error"); }; /*send the request to server*/ xhr.send(); } //For template-1 var dateTemplate = document.getElementById(templId).innerHTML; var template = Handlebars.compile(dateTemplate); sendGet(function (response) { document.getElementById(finId).innerHTML += template(response); }) } /* --- addContent.js --- */ var addContent = require('./contentProcess'); addContent("../data/webContent.json", "main-template", 'maintext'); 
 /* --- main.js (послС компиляции addContent.js с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ browserify) --- */ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ var addContent = require('./contentProcess'); /*MAINBLOCK*/ addContent("../data/webContent.json", "main-template", 'maintext'); },{"./contentProcess":2}],2:[function(require,module,exports){ module.exports = function(jsonDir, templId, finId){ function sendGet(callback) { /* create an AJAX request using XMLHttpRequest*/ var xhr = new XMLHttpRequest(); /*reference json url taken from: http://www.jsontest.com/*/ /* Specify the type of request by using XMLHttpRequest "open", here 'GET'(argument one) refers to request type "http://date.jsontest.com/" (argument two) refers to JSON file location*/ xhr.open('GET', jsonDir); /*Using onload event handler you can check status of your request*/ xhr.onload = function () { if (xhr.status === 200) { callback(JSON.parse(xhr.responseText)); } else { alert(xhr.statusText); } }; /*Using onerror event handler you can check error state, if your request failed to get the data*/ xhr.onerror = function () { alert("Network Error"); }; /*send the request to server*/ xhr.send(); } //For template-1 var dateTemplate = document.getElementById(templId).innerHTML; var template = Handlebars.compile(dateTemplate); sendGet(function (response) { document.getElementById(finId).innerHTML += template(response); }) } },{}]},{},[1]); 
 <!-- Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚ ΠΊΠΎΠ΄Π° ΠΈΠ· index.html (Π½Π° страницС ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ bootstrap, Ссли это ΠΈΠΌΠ΅Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅) --> <div id="maintext"> </div> <script id="main-template" type="text/x-handlebars-template"> {{{header}}} {{{describtion}}} {{{sentence3}}} </script> 

  • one
    Too many details. Try to make a minimal reproducible example, then it will be easier to understand what the problem is for both you and us. - Duck Learns to Hide
  • @ Duck Yes, I was just going to do it. - Rumata
  • @ Duck But I'm afraid it will not be much easier - here I basically removed all unnecessary ... but I will make a separate version and add a link to download. - Rumata
  • @ Duck Created a theme where you can download a working example and with a non-working one (with a reduced version of the web page to which I try to apply the same method). ru.stackoverflow.com/q/562324/195703 - Rumata

1 answer 1

(maybe not lucky, but I'll try to guess)

 /* --- webContent.json --- */ { header: "Powerful business", describtion: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec turpis neque, sodales a faucibus at, viverra luctus urna. Suspendisse dignissim neque dui, in tincidunt arcu.' } 

This is not JSON, in JSON, keys without quotes are not allowed and all quotes should be just double, it should be so

 { "header": "Powerful business", "describtion": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec turpis neque, sodales a faucibus at, viverra luctus urna. Suspendisse dignissim neque dui, in tincidunt arcu." } 

/* ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ */ in JSON are also prohibited, yes.

  • Yes, I also noticed, that's right, thank you. But after the correction, it still does not work. I am preparing a simplified example. - Rumata
  • @MaximVelichkin So after the correction, the idea is that there should be another error, and if so, then this is another question - andreymal
  • Ok, then the answer is accepted, thanks. - Rumata
  • Created a theme where you can download a working example and with a non-working one (with a reduced version of the web page to which I try to apply the same method). ru.stackoverflow.com/q/562324/195703 - Rumata
  • Thanks again for the fix in JSON syntax, but the error, the screenshot of which I placed above after the fix remains ... As I understand it, the console complains about this line: callback(JSON.parse(xhr.responseText)); - Rumata