开发者

Javascript in a website

开发者 https://www.devze.com 2023-02-18 10:46 出处:网络
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.

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?


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消