dukevin
dukevin

Reputation: 23198

Javascript in a website

This is a basic question but google didn't provide any help.

I have a website and what to beable to run javascript on it.

In my directories I have index.html, and index.css. For the javascript file, I'm assuming it should be called index.js.

In my index.js file I have this:

var countTime = 0; // Number of seconds
var redirectURL = "http://example.com"; // URL to direct to

countTime = (countTime+1)*1000;
function updateCount(){
    countTime = countTime-1000;
    if(document.getElementById("countdownDisplay"))
        document.getElementById("countdownDisplay").innerHTML = (countTime/1000);

    if(countTime <= 0)
        location.href = redirectURL;
    else
        setTimeout("updateCount()",1000);
}

updateCount();

However it's not working when I visit the page with a browser. Do I have to do something in my html file like include index.js or something?

Upvotes: -1

Views: 89

Answers (3)

Jakob Alexander Eichler
Jakob Alexander Eichler

Reputation: 3056

basically when trying to write some html, you can either search on google how to write the code or as well search for a page which provides what you want to do and look into it's source. This way google would have helped you, because google uses javascript.

In addition, check your totalvalidator. It is a very useful firefox plugin for advanced html validation. It supports better evaluation than the w3c validator does.

Upvotes: 0

anon
anon

Reputation: 1061

Yes, you need to include it in the HTML file. Here are some instructions.

Upvotes: 0

Raynos
Raynos

Reputation: 169531

<script src="index.js" type="text/javascript"></script>

Should go in your <head>.

This will load the script for you and then the code gets executed.

Your also going to need something like

<div id="countdownDisplay"></div> in your <body> for the countdown to work.

Whilst I'm at it you probably want a

<style src="index.css" type="text/css"></style> in your <head> as well if you havn't already.

Upvotes: 5

Related Questions