0

I have couple of identical html pages and I would like to use the same JavaScript file which reads text files and changes data on the html pages. As the text files are different for every page I have done it with multiple if statements. It would be better if there is a way to replace it with some loop?

if (window.location.pathname=='/myapp/main.html') {
  $.get('data/data1.txt', function(data) {
     var bigarray = data.split('\n');
     bigarray.forEach(function(currRow){
       currentRow = currRow.split(';');
       table.push(currentRow);});
  }, 'text');
}

if (window.location.pathname=='/myapp/main2.html') {
  $.get('data/data2.txt', function(data) {
     var bigarray = data.split('\n');
     bigarray.forEach(function(currRow){
       currentRow = currRow.split(';');
       table.push(currentRow);});
  }, 'text');
}

if (window.location.pathname=='/myapp/main3.html') {
  $.get('data/data3.txt', function(data) {
     var bigarray = data.split('\n');
     bigarray.forEach(function(currRow){
       currentRow = currRow.split(';');
       table.push(currentRow);});
  }, 'text');
}

if (window.location.pathname=='/myapp/main4.html') {
  $.get('data/data4.txt', function(data) {
     var bigarray = data.split('\n');
     bigarray.forEach(function(currRow){
       currentRow = currRow.split(';');
       table.push(currentRow);});
  }, 'text');
}

Thank you in advance!

4
  • 1
    you can write a simple function which returns your text file path based on html path, thats it. Commented May 21, 2018 at 19:37
  • I second what @RahulR. said. In addition to that, use .map or .reduce instead of .forEach Commented May 21, 2018 at 19:38
  • switch statements are easier to read than multiple if statements too. Commented May 21, 2018 at 19:39
  • Actually a .forEach makes sense since the OP does not need to manipulate the array. 'For each item do something unrelated to said item' Commented May 21, 2018 at 19:54

4 Answers 4

4

So either use an object

var paths = {
 "/myapp/main.html" : "data/data1.txt",
 "/myapp/main2.html" : "data/data2.txt",
 "/myapp/main3.html" : "data/data3.txt",
 "/myapp/main4.html" : "data/data4.txt"
};

var filePath = paths[window.location.pathname];
if (filePath) {
  $.get(filePath, ...)
}

or use a reg exp to match the number and use that if the paths are the same.

Sign up to request clarification or add additional context in comments.

2 Comments

Beat me to it. But the important thing is "why loop if you don't have to?". This, or a regex, seem like preferred ways of doing it.
No, no, sorry, the OP had asked "It would be better if there is a way to replace it with some loop?", that's what I was referring to.
2

Since your paths have the same structure, you could simply use a regex to get the number.

With that you can create new pages/data as you go, and it will just pick them up automatically.

if (window.location.pathname.startsWith('/myapp/main') {  // this "if" might not be needed

  var pathnum = window.location.pathname.replace(/[^0-9]/g,'');

  $.get('data/data' + pathnum + '.txt', function(data) {
     var bigarray = data.split('\n');
     bigarray.forEach(function(currRow){
       currentRow = currRow.split(';');
       table.push(currentRow);});
  }, 'text');

}

And if no number as in main.html should use data1.txt, you can do something like this

  $.get('data/data' + (pathnum == '' ? 1 : pathnum) + '.txt', function(data) {

Comments

1

Created a simple reusable function that accepts a path and a data url:

const getData = (path, url) => {
  if (window.location.pathname === path ) {
    $.get(url, function(data) {
       var bigarray = data.split('\n');
       bigarray.forEach(function(currRow){
         currentRow = currRow.split(';');
         table.push(currentRow);});
    }, 'text');
  }
}

// Usage
getData('/myapp/main.html', 'data/data1.txt');
getData('/myapp/main2.html', 'data/data2.txt');
getData('/myapp/main3.html', 'data/data3.txt');
getData('/myapp/main4.html', 'data/data4.txt');

Comments

1

You could use template literals.

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

function changeText() {
    const toString = Object.prototype.toString;
    const { pathname } = window.location;

    // Get the index of the dot
    const dot = pathname.search('.');

    // Grab the character immediately preceding the dot
    const testChar = pathname.charAt(dot-1);

    // If the character is not a number, set the index to 1
    const index = toString.call(testChar) === '[object Number]'
        ? testChar
        : 1;

  // Use the template literal
  $.get(`data/data${index}.txt`, function(data) {
     var bigarray = data.split('\n');
     bigarray.forEach(function(currRow){
       currentRow = currRow.split(';');
       table.push(currentRow);});
  }, 'text');

}

The method requires no looping or custom text. Usage is simply: changeText().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.