[英]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.