繁体   English   中英

HTML不调用外部JS文件

[英]HTML not calling external JS file

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>

当与HTML内联时,javascript可以工作,但是当尝试从外部文件中调用它时,则不能工作。

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

如果从外部文件调用JS,则需要删除<SCRIPT> </ SCRIPT>标记,否则会出现错误。

您的js文件必须仅包含Javascript代码:

function validateadlocation()
{
    var checkboxs=document.getElementsByName("location");
    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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM