-1

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?

dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Mar 17, 2011 at 2:11
1
  • 2
    FYI, you can name these files any way you like. Naming an HTML file index.html will make it the default page for that directory, but your CSS and JS file's names only have meanings to you. I'd suggest morem meaningful names so it's easier to tell what file contains what code. This gets more important as your site becomes more complex. Commented Mar 17, 2011 at 2:18

3 Answers 3

5

<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.

answered Mar 17, 2011 at 2:14
Sign up to request clarification or add additional context in comments.

2 Comments

so do I even have a js file or do I actually put this in the HTML?
@KevinDuke keep your js file. You just need to include a script tag with a src with your file location in your HTML file.
0

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

answered Mar 17, 2011 at 2:14

Comments

0

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.

answered Mar 17, 2011 at 2:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.