innerHTML generated content shows in Chrome but not in Firefox or Edge
the timeWorkedTotal.innerHTML content generated by JS shows up in Chrome but is empty in Firefox (and Edge). All other .innerHTML content shows without problem in either browser. If possible, I would like to resolve this using vanilla JS/html/css without using any libraries unless it is completely unavoidable.
I have tried to change innerHTML to alternatives like innerText and textContent but it did not help. I have checked the cross browser compatibility at https://www.quirksmode.org/dom/html/ and it seems that innerHTML should work fine (I am not concerned with security at this point since picked up data is hard-coded).
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
]
//A function that calculates the total time work at a job using collected start and end dates
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = new Date(jobData.dateStart).getTime();
end = new Date(jobData.dateEnd).getTime();
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year")
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year")
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year")
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years")
}
}}
//Function that creates html elements for and display user input data related to work experience information
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}}
The innerHTML from the if else if statement for timeWorkedTotal does not show in the firefox although empty parent div and a holding span are generated with no problem.
javascript html firefox cross-browser innerhtml
add a comment |
the timeWorkedTotal.innerHTML content generated by JS shows up in Chrome but is empty in Firefox (and Edge). All other .innerHTML content shows without problem in either browser. If possible, I would like to resolve this using vanilla JS/html/css without using any libraries unless it is completely unavoidable.
I have tried to change innerHTML to alternatives like innerText and textContent but it did not help. I have checked the cross browser compatibility at https://www.quirksmode.org/dom/html/ and it seems that innerHTML should work fine (I am not concerned with security at this point since picked up data is hard-coded).
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
]
//A function that calculates the total time work at a job using collected start and end dates
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = new Date(jobData.dateStart).getTime();
end = new Date(jobData.dateEnd).getTime();
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year")
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year")
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year")
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years")
}
}}
//Function that creates html elements for and display user input data related to work experience information
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}}
The innerHTML from the if else if statement for timeWorkedTotal does not show in the firefox although empty parent div and a holding span are generated with no problem.
javascript html firefox cross-browser innerhtml
1
jobData.dateStartis"June 2016"you thennew Date("June 2016")which will fail as the input is not valid, the subsequentgetTime()returnsNaNwhich poisons all the related math and causes none of your if/else to match.
– Alex K.
Jan 18 at 14:07
add a comment |
the timeWorkedTotal.innerHTML content generated by JS shows up in Chrome but is empty in Firefox (and Edge). All other .innerHTML content shows without problem in either browser. If possible, I would like to resolve this using vanilla JS/html/css without using any libraries unless it is completely unavoidable.
I have tried to change innerHTML to alternatives like innerText and textContent but it did not help. I have checked the cross browser compatibility at https://www.quirksmode.org/dom/html/ and it seems that innerHTML should work fine (I am not concerned with security at this point since picked up data is hard-coded).
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
]
//A function that calculates the total time work at a job using collected start and end dates
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = new Date(jobData.dateStart).getTime();
end = new Date(jobData.dateEnd).getTime();
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year")
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year")
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year")
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years")
}
}}
//Function that creates html elements for and display user input data related to work experience information
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}}
The innerHTML from the if else if statement for timeWorkedTotal does not show in the firefox although empty parent div and a holding span are generated with no problem.
javascript html firefox cross-browser innerhtml
the timeWorkedTotal.innerHTML content generated by JS shows up in Chrome but is empty in Firefox (and Edge). All other .innerHTML content shows without problem in either browser. If possible, I would like to resolve this using vanilla JS/html/css without using any libraries unless it is completely unavoidable.
I have tried to change innerHTML to alternatives like innerText and textContent but it did not help. I have checked the cross browser compatibility at https://www.quirksmode.org/dom/html/ and it seems that innerHTML should work fine (I am not concerned with security at this point since picked up data is hard-coded).
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
]
//A function that calculates the total time work at a job using collected start and end dates
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = new Date(jobData.dateStart).getTime();
end = new Date(jobData.dateEnd).getTime();
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year")
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year")
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year")
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years")
}
}}
//Function that creates html elements for and display user input data related to work experience information
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}}
The innerHTML from the if else if statement for timeWorkedTotal does not show in the firefox although empty parent div and a holding span are generated with no problem.
javascript html firefox cross-browser innerhtml
javascript html firefox cross-browser innerhtml
edited Jan 18 at 14:20
Regina Zaripova
asked Jan 18 at 13:52
Regina ZaripovaRegina Zaripova
62
62
1
jobData.dateStartis"June 2016"you thennew Date("June 2016")which will fail as the input is not valid, the subsequentgetTime()returnsNaNwhich poisons all the related math and causes none of your if/else to match.
– Alex K.
Jan 18 at 14:07
add a comment |
1
jobData.dateStartis"June 2016"you thennew Date("June 2016")which will fail as the input is not valid, the subsequentgetTime()returnsNaNwhich poisons all the related math and causes none of your if/else to match.
– Alex K.
Jan 18 at 14:07
1
1
jobData.dateStart is "June 2016" you then new Date("June 2016") which will fail as the input is not valid, the subsequent getTime() returns NaN which poisons all the related math and causes none of your if/else to match.– Alex K.
Jan 18 at 14:07
jobData.dateStart is "June 2016" you then new Date("June 2016") which will fail as the input is not valid, the subsequent getTime() returns NaN which poisons all the related math and causes none of your if/else to match.– Alex K.
Jan 18 at 14:07
add a comment |
1 Answer
1
active
oldest
votes
Note: parsing of date strings with the Date constructor (and
Date.parse, they are equivalent) is strongly discouraged due to
browser differences and inconsistencies.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If the structure of dateStart and dateEnd is fix, you could use a litte function to convert the Strings in proper Date-Objects.
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
and use that in your function daysBetween
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
];
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year");
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year");
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year");
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years");
}
}
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
}
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
}
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}<div id="exp_details"></div>add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255417%2finnerhtml-generated-content-shows-in-chrome-but-not-in-firefox-or-edge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Note: parsing of date strings with the Date constructor (and
Date.parse, they are equivalent) is strongly discouraged due to
browser differences and inconsistencies.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If the structure of dateStart and dateEnd is fix, you could use a litte function to convert the Strings in proper Date-Objects.
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
and use that in your function daysBetween
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
];
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year");
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year");
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year");
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years");
}
}
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
}
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
}
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}<div id="exp_details"></div>add a comment |
Note: parsing of date strings with the Date constructor (and
Date.parse, they are equivalent) is strongly discouraged due to
browser differences and inconsistencies.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If the structure of dateStart and dateEnd is fix, you could use a litte function to convert the Strings in proper Date-Objects.
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
and use that in your function daysBetween
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
];
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year");
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year");
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year");
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years");
}
}
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
}
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
}
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}<div id="exp_details"></div>add a comment |
Note: parsing of date strings with the Date constructor (and
Date.parse, they are equivalent) is strongly discouraged due to
browser differences and inconsistencies.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If the structure of dateStart and dateEnd is fix, you could use a litte function to convert the Strings in proper Date-Objects.
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
and use that in your function daysBetween
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
];
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year");
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year");
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year");
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years");
}
}
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
}
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
}
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}<div id="exp_details"></div>
Note: parsing of date strings with the Date constructor (and
Date.parse, they are equivalent) is strongly discouraged due to
browser differences and inconsistencies.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If the structure of dateStart and dateEnd is fix, you could use a litte function to convert the Strings in proper Date-Objects.
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
and use that in your function daysBetween
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
];
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year");
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year");
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year");
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years");
}
}
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
}
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
}
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}<div id="exp_details"></div>let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
];
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year");
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year");
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year");
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years");
}
}
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
}
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
}
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}<div id="exp_details"></div>let jobData = [
{
dateStart: "June 2016",
dateEnd: "October 2018",
dates: "June 2016 – October 2018",
employer: "Tim Hortons",
title: "Cashier"
},
{
dateStart: "January 2013",
dateEnd: "March 2017",
dates: "January 2013 – March 2017",
employer: "Café Bar Main Street",
title: "Server"
},
{
dateStart: "June 2015",
dateEnd: "May 2017",
dates: "June 2015 – May 2017",
employer: "Café Bar Main Street",
title: "Server"
},
];
for (let i = 0; i <= jobData.length - 1; i++) {
function daysBetween(jobData){
let one_day = 1000 * 60 * 60 * 24;
start = convertDateStr(jobData.dateStart);
end = convertDateStr(jobData.dateEnd);
timeWorked = (end - start);
let timeWorkedYears = (timeWorked / one_day /365).toFixed(2);
let expDetails = document.getElementById('exp_details');
let timeWorked2 = document.createElement('div');
let timeWorkedTotal = document.createElement('span');
expDetails.appendChild(timeWorked2);
timeWorked2.appendChild(timeWorkedTotal);
timeWorked2.classList.add('time_worked');
timeWorkedTotal.classList.add('time_worked_total');
//condition checks the length of employment and assigns proper "year/years"
if(timeWorkedYears < 1.00) {
timeWorkedTotal.innerHTML = ("less than a year");
} else if (timeWorkedYears == 1.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + " year");
} else if(timeWorkedYears > 1.00 && timeWorkedYears < 2.00) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ year");
} else if(timeWorkedYears >= 2) {
timeWorkedTotal.innerHTML = (Math.round(timeWorkedYears) + "+ years");
}
}
// convert the date strings into Date-Objects
function convertDateStr(s) {
var m = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
var sArr = s.split(' ');
return new Date(sArr[1],m.indexOf(sArr[0])).getTime();
}
}
for (let i = 0; i <= jobData.length - 1; i++) {
experienceData(jobData[i]);
daysBetween(jobData[i]);
}
function experienceData(jobData) {
let expDetails = document.getElementById('exp_details');
// empty div is created below that will hold all the of the data for job history
let jobDiv = document.createElement('div');
// new div is appended to the existing parent div
expDetails.appendChild(jobDiv);
// children of the jobDiv are created to hold dates, employer name and job title
let jobDates = document.createElement('span');
let employer = document.createElement('span');
let title = document.createElement('span');
// all children are appended to their respective parents
jobDiv.appendChild(jobDates);
jobDiv.appendChild(employer);
jobDiv.appendChild(title);
//class names are added to newly created elements in order to apply styling to them
jobDiv.classList.add('job_parent');
jobDates.classList.add('job_dates');
employer.classList.add('employer');
title.classList.add('job_title');
//empty elements pick up information from the provided data file
jobDates.innerHTML = jobData.dates;
employer.innerHTML = jobData.employer;
title.innerHTML = jobData.title;
}<div id="exp_details"></div>answered Jan 18 at 16:37
OlafantOlafant
1437
1437
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255417%2finnerhtml-generated-content-shows-in-chrome-but-not-in-firefox-or-edge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
jobData.dateStartis"June 2016"you thennew Date("June 2016")which will fail as the input is not valid, the subsequentgetTime()returnsNaNwhich poisons all the related math and causes none of your if/else to match.– Alex K.
Jan 18 at 14:07