HTML - 链接外部Javascript文件

时间:2014-10-01 11:33:56

标签: javascript html

我无法弄清楚为什么我的外部Javascript文件没有被我的HTML文件链接。 我有

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

在我的HTML标题中

window.onload = function () { alert("Working");

};

在我的Javascript文件中。

我正在使用firefox扩展Firebug,它告诉我我的网页没有加载我的Javascript文件。 我究竟做错了什么? 感谢

编辑:我已多次检查路径,我现在已将.js文件移动到同一文件夹。

<!DOCTYPE html>

&LT; html lang =“en”&gt;

<meta charset="utf-8" />
<meta name="description"    content="Home Page" />
<meta name="author"         content="Caitlin Reeve" />
<link rel="Stylesheet" type="text/css" href="styles/style.css" />
<script type="text/javascript" src="jobs.js"></script>
<Title> Home Page</Title>

<!-- Image Source: http://cdn.business2community.com/wp-content/uploads/2013/03/Hat3.png-->

<a name="logo" href= "index.html"><img src="images/Hat3.png" alt="Business Logo" /></a>
<h1>Tech Solutions </h1>
<hr />

<!-- Site Menu -->
<nav>
<p></p>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="job1.html" >Jobs</a></li>
<li><a href="enquire.html" >Enquire</a></li>
<li><a href="about.html">About</a></li>
</ul>

</nav>
<h3> Application </h3>

<form action="http://mercury.ict.swin.edu.au/it000000/formtest.php" name="applyJob" method="POST" >
    <fieldset>
    <legend> Your Details </legend>
    Job Reference Number <input required="required" name="jobno" type="text" required="required" /><br />
    First Name <input required="required" name="fname" type="text" pattern="[A-Za-z]{1,15}" maxlength="15" required="required" /><br />
    Last Name <input required="required" name="lname" type="text" pattern="[A-Za-z]{1,25}" maxlength="25" required="required"/><br />
    Date of Birth <input required="required" name="dob" type="date" pattern="[(0[1-9]|[12][0-9]|3[01])[/]((0[1-9]|1[012])[/](19|20)\d\d][(0[1-9]|[12][0-9]|3[01])[/]((0[1-9]|1[012])[/][(0[0-9]|1[0-9]\d\d)]" required="required"/><br />
    Gender  
        <label><input type="radio" value="Male" name="gender" checked="checked" /> Male </label>

        <label><input type="radio" name="gender" value="Female" /> Female </label> <br />
    <p>
    Address: <br />
    Street Address <input required="required" name="streetad" type="text" maxlength="50" required="required" /><br />
    Suburb/Town <input required="required" name="subad" type="text" maxlength="25" required="required" /><br />
    State <select required="required" name="state">
        <option value="">Choose</option>
        <option value="WA">WA</option>
        <option value="NT">NT</option>
        <option value="SA">SA</option>
        <option value="QLD">QLD</option>
        <option value="NSW">NSW</option>
        <option value="VIC" selected="selected" >VIC</option>
        <option value="ACT">ACT</option>
        <option value="TAS">TAS</option>
        </select>
    </p>

    <br />
    Postcode <input required="required" name="postcode" type="text" pattern="[0-9]{4}" required="required"/><br />
    <br />
    Email Address <input required="required" name="email" type="email" required="required" /><br />
    Phone Number <input required="required" name="phone" type="tel" pattern="[0-9]{10}" maxlength="10" required="required"/><br />

    <p>
    Skills <br />
        <label><input required="required" type="checkbox" name="HTML" value="html" required="required" /> HTML experience</label><br />

        <label><input required="required" type="checkbox" name="Degree" value="degree" required="required" /> Degree (Bachelor or higher) </label> <br />

        <label><input required="required" type="checkbox" name="SQL" value="sql" required="required" /> SQL experience </label> <br />

        <label><input type="checkbox" name="lift" value="lift" /> Able to lift heavy objects </label> <br />

        <label><input type="checkbox" name="experience" value="years" /> 3 or more years of experience </label> <br />

        <label><input type="checkbox" name="RubyPython" value="languages" /> Ruby or Python knowledge </label> <br />

        <label><input type="checkbox" name="other" value="other" /> Other Skills </label>



    </p>

    Other Skills <br />
    <textarea rows="3" COLS="25"></textarea><br />

FPS

        <!--Submits the form to the server-->
    <input type="submit" value="Submit Application" />


    </fieldset>
</form>


2014

3 个答案:

答案 0 :(得分:0)

如果你的路径是好的并且js文件确实加载了,有一件事需要解决:onload监听器需要是一个函数:

window.onload = function(){
    alert("Working");
}

答案 1 :(得分:0)

您的js文件必须与html放在同一个文件夹中。

html/
  |__myHtml.html
  |__jobs.js

如果它在其他文件夹中,例如:

html/
  |__myHtml.html

js/
  |__jobs.js

然后更改javascript的相对路径:

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

答案 2 :(得分:0)

看起来你错过了function()调用。看下面修改过的代码很快就在firefox中测试了它并且它有效。你的外部链接看起来很好。

window.onload = function () { alert("Working");
};