网站上的Javascript

duk*_*vin 0 javascript web

这是一个基本问题,但谷歌没有提供任何帮助.

我有一个网站,可以在其上运行javascript.

在我的目录中,我有index.html和index.css.对于javascript文件,我假设它应该被称为index.js.

在我的index.js文件中,我有这个:

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();
Run Code Online (Sandbox Code Playgroud)

但是,当我使用浏览器访问该页面时,它无法正常工作.我是否必须在我的html文件中执行某些操作,例如include index.js或其他什么内容?

Ray*_*nos 6

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

应该进入你的<head>.

这将为您加载脚本,然后执行代码.

你也需要类似的东西

<div id="countdownDisplay"></div>在你<body>的倒计时工作.

虽然我在这里你可能想要一个

<style src="index.css" type="text/css"></style>在你的<head>,以及如果你没有带了.