Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm new here so if you see anything off about my question or 'etiquette' please let me know!

I'm trying to create a personalized new tab extension in Google Chrome, and have a problem with JavaScript time code appearing. Though it works in Brackets, the compiler I use, the time never displays whenever I load the extension folder onto Chrome. After searching around, I learned that this was because I had my Javascript inline rather than in an external file, and so I naturally attempted to move my script into a time.js file. I am not totally sure about how to show the time upon the new tab loading, but I thought I'd show y'all my attempts.

My index.html file, disregarding irrelevant code, shows as:

<html>
<head>
    <title>New Tab</title>
    <script type = "text/javascript" src = "time.js"></script>
</head>
<body onload = "startTime()">
    <div id = "time"></div>
    </div>
</body>

^ Can't see closing html tag for some reason. Anyway, here is my time.js file, using some changed up w3school code:

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    h = minusTwelve(h);
    m = checkTime(m);
    document.getElementById('time').innerHTML =
    h + ":" + m;
    var t = setTimeout(startTime, 500);} 
function minusTwelve(i) {
if (i > 12) {i = i - 12};
if (i == 0) {i = 12};
return i;

function minusTwelve(i) {
if (i > 12) {i = i - 12};
if (i == 0) {i = 12};
return i;}

function checkTime(i) {
if (i < 10) {i = "0" + i};
return i;}

(sorry about the indents in those last two functions, something went weirdly :P)

Anyway, I have this so far and I am not sure how to integrate the time when the tab loads in Chrome. I have seen some people mention addEventListener or something for clicks and whatnot, but I am not sure how this would apply here. Also, in the body of my HTML is the onload call, and though I would think this would take care of it, it seems to not have :/. I would really appreciate your help.

Thanks!

share|improve this question

Are you able to see any errors in the Javascript console?

Also one thing I noticed is that, you have missed the closing braces for your first function minusTwelve()

See if it fixes the issue

And have you mentioned your script file in the manifest file? Check this docs

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.