Reputation: 353
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!
Upvotes: 0
Views: 2161
Reputation: 958
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()
.
Upvotes: 1
Reputation: 87201
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) {
Upvotes: 2
Reputation: 16441
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');
Upvotes: 1
Reputation: 207521
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.
Upvotes: 4