I'm making a google chrome extension that simply displays the current date and time whenever you open a new tab. So far, it works only if I have each function on separate scripts (each function that displays the date or time". In an attempt to reduce the number of files, I'm trying to put both functions in the same script. My problem is that I can't seem to call the specific functions from the same script file. I've been googling this for longer than I should and it's driving me crazy. Here is my code. Any help would be greatly appreciated!
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type= "text/javascript" src ="script".js></script>
</head>
<body bgcolor="C1E9E1">
<div class="boxDate">
<script>displayDate();</script>
</div>
<br>
<br>
<div class="boxTime">
<script>displayTime();</script>
</div>
</body>
function checkNum(i){
if(i<10) i = "0"+i;
return i;
}
function displayDate() {
var day = new Date();
var yr = day.getFullYear().toString().substr(-2);
var mn = day.getMonth() + 1;
var dy = day.getDate();
yr = checkNum(yr);
mn = checkNum(mn);
dy = checkNum(dy);
var dateFormat = mn + " " + dy + " " + yr;
t = setTimeout(displayDate, 500);
return dateFormat;
//document.getElementById("dateOutput").innerHTML = dateFormat;
}
function displayTime(){
var time = new Date();
var hr = time.getHours() % 12 || 12;
var mi = time.getMinutes();
var sc = time.getSeconds();
hr = checkNum(hr);
mi = checkNum(mi);
sc = checkNum(sc);
var timeFormat = hr + ":" + mi + ":" + sc;
t = setTimeout(displayTime, 500);
return timeFormat;
//document.getElementById("timeOutput").innerHTML = timeFormat;
}
Notes: the boxDate and boxTime class are from a css file My code did work when I did this, but like I said, this would involve me having to have 2 different js files.
<div class="boxDate">
<input type="text" id="dateOutput">
script src = "displayDate.js"></script>
</div>
<div class="boxTime">
<input type="text" id="timeOutput">
<script src = "displayTime.js"></script>
</div>
line but that didn't change anything either. Would placement of that line matter in this case?
– xerop Feb 03 '18 at 04:05