javascript - HTML不调用外部JS文件

标签 javascript html

HTML:

<!DOCTYPE html>
<html>
    <head>
        <script src="adlocationverify.js"></script>
        <!--other script and also external css included over here-->
    </head>

<body>
    <form>
        Sales Rep: <input type="text" name="salesrep" required><br>
        Customer Name: <input type="text" name="customer" required><br>
        Target URL: <input type="text" name="url1" required><br>
        Target URL: <input type="text" name="url2"><br>
        Target URL: <input type="text" name="url3"><br>
        Number of Impressions: <input type="number" name="impressions" min="100000" required><br>
        Start Date: <input type="date" name="startdate" required><br>
        End Date: <input type="date" name="enddate" required><br>
        Geo Target (Optional): <input type="text" name="geo"><br>
        Technologies (Optional): <input type="text" name="tags"><br>
        Ad Locations: <input type="checkbox" name="top"> Top Leaderboard <input type="checkbox" name="middle"> Middle Leaderboard <input type="checkbox" name="sidebar"> Sidebar<br>
        <input type="submit" name="submit" onClick="validateadlocation()">
    </form>
</body>
</html>

JS:

<script>
function validateadlocation()
{
    var checkboxs=document.getElementsByName("top middle sidebar");
    var okay=false;
    for(var i=0,l=checkboxs.length;i<l;i++)
    {
        if(checkboxs[i].checked)
        {
            okay=true;
        }
    }
    if(okay)alert("Thank you for checking a checkbox");
    else alert("Please check a checkbox");
}
</script>

JavaScript 在嵌入 HTML 时可以工作,但在尝试从外部文件调用它时则无法工作。

最佳答案

js 文件不应包含 <script></script>标签。标签只能包含在 html 文件中。

关于javascript - HTML不调用外部JS文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25729717/

相关文章:

javascript - 如何在url中添加变量

java - 字符串编码不输出所有字符

javascript - html 5 历史记录恢复了一些更改而不是其他更改?

javascript - HTML5相机缓冲和延迟(延迟镜像)

javascript - 交易解码结果乱码

javascript - 使文本内容在 UWP Javascript 中可选择

html - 为什么 Foundation 建议在主要内容中添加 tabindex ="0"?

html - css - 我无法移动导航菜单

javascript - 更改 Twitter Feed 中的 Twitter Logo

javascript - 从其他文件获取数据库信息