简体   繁体   English

不加载外部JS文件

[英]Not Loading External JS File

There is no real reason that this code shouldn't work, as far as I can tell. 据我所知,没有真正的理由该代码不起作用。 I have a HTML file and an external .js file. 我有一个HTML文件和一个外部.js文件。 Here is the statement that I am using inside of the html to load the .js file: 这是我在html内部用于加载.js文件的语句:

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

The name of the file is correct, and it is in the same directory. 文件名正确,并且在同一目录中。 I'll post the full code of the html page below, just in case something is off with something else. 我将在下面发布html页面的完整代码,以防万一其他问题解决了。

Is there anything that I am missing? 有什么我想念的吗? The .js file should make a sliding menu, however it is simply not loading. .js文件应该创建一个滑动菜单,但是根本不会加载。 I have an alert inserted in the .js file that should alert me when it's working, although it doesn't show up. 我在.js文件中插入了一个警报,尽管它没有显示,但应该在工作时向我发出警报。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>

<title>The Jones Family</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="family.css" type="text/css" />
<script type="text/javascript" src="family.js"></script>
<script language="JavaScript" type="text/javascript">
    var d=new Date();
    var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
    //Ensure correct for language. English is "January 1, 2004"
    var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
    alert ("Inside script is working");
</script>
</head>
<body >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="head">
    <td width="382" colspan="3" rowspan="2"><img src="camera.jpg" alt="camera" border="0" /></td>
    <td width="378" height="50" colspan="3" id="logo" valign="bottom" align="center" nowrap="nowrap">The Jones Family Website</td>
    <td width="100%">&nbsp;</td>
  </tr>

  <tr class="head">
    <td height="51" colspan="3" id="tagline" valign="top" align="center">Photo Gallery</td>
    <td width="100%">&nbsp;</td>
  </tr>

    <tr>
    <td colspan="7" class="separator" ><div class="myHr"><hr /></div></td>
  </tr>

  <tr class="separator">
    <td colspan="7" id="dateformat" height="20">&nbsp;&nbsp;<script language="JavaScript" type="text/javascript">
      document.write(TODAY);    </script>   </td>
  </tr>
  <tr>
    <td colspan="7" class="separator" ><div class="myHr"><hr /></div></td>
  </tr>

  <tr>
    <td width="200" height="450" valign="top" class="nav" >
    <table class="nav" border="0" cellspacing="0" cellpadding="0" width="40" id="navigation">
        <tr>
          <td >&nbsp;<br />
         &nbsp;<br /></td>
        </tr>
        <tr>
          <td ><div class="navG" id="gallery1" style="position: absolute; top: 180px; left: -150px; " >
                 <table width="184" border="0" cellspacing="0" cellpadding="0">
                   <tr>
                     <td width="150"><a href="#">Disney</a><br />
                         <a href="#">SeaWorld</a><br />
                         <a href="#">Bush Garden</a><br />
                         <a href="#">Universal Studios</a><br />
                     </td>
                     <td><img src="gallery1.jpg" alt="gallery 1" class="nav1" /></td>
                   </tr>
                 </table>
              </div></td>
        </tr>

        <tr>
          <td ><div class="navG" id="gallery2" style="position: absolute; top: 285px; left: -150px; " >
                 <table width="184" border="0" cellspacing="0" cellpadding="0">
                   <tr>
                     <td width="150"><a href="#">Sweet 16</a><br />
                         <a href="#">Summer Camp</a><br />
                         <a href="#">Holiday</a><br />
                         <a href="#">Ski Trip</a><br />
                     </td>
                     <td><img src="gallery2.jpg" alt="gallery 2" class="nav1" /></td>
                   </tr>
                 </table>
              </div></td>
        </tr>

        <tr>
          <td ><div class="navG" id="gallery3" style="position: absolute; top: 390px; left: -150px; "  >
                 <table width="184" border="0" cellspacing="0" cellpadding="0">
                   <tr>
                     <td width="150"><a href="#">Mom's Birthday (2011)</a><br />
                         <a href="#">Dad's Birthday (2011)</a><br />
                         <a href="#">Bobby's Birthday</a><br />
                         <a href="#">Jenny's</a><br />
                     </td>
                     <td><img src="gallery3.jpg" alt="gallery 3" class="nav1" /></td>
                   </tr>
                 </table>
              </div></td>
        </tr>

        <tr>
          <td ><div class="navG" id="gallery4" style="position: absolute; top: 495px; left: -150px; " >
                 <table width="184" border="0" cellspacing="0" cellpadding="0">
                   <tr>
                     <td width="150"><a href="#">Skatepark</a><br />
                         <a href="#">Wildlife</a><br />
                         <a href="#">Beach</a><br />
                         <a href="#">Hawaii</a><br />
                     </td>
                     <td><img src="gallery4.jpg" alt="gallery 4" class="nav1" /></td>
                   </tr>
                 </table>
                </div></td>
        </tr>
      </table>
     <br />
    &nbsp;<br />
    &nbsp;<br />
    &nbsp;<br />    </td>
    <td width="50"></td>
    <td width="305" colspan="2" valign="top"><br />
    &nbsp;<br />
    &nbsp;<br />
    <table border="0" cellspacing="0" cellpadding="0" width="305">
        <tr>
          <td class="pageName">Family Photos</td>
        </tr>

        <tr>
          <td class="bodyText"><p>From here you can see family Photos taken by our various amateur photographers!</p>

        <p>We hope you enjoy the photos as much as we do!</p>
                <p align="center"><img src="sayCheese.jpg" alt="Say Cheese" /></p></td>
        </tr>
      </table>
     <br />
    &nbsp;<br />    </td>
    <td width="50"></td>
        <td width="190" valign="top"><br />
        &nbsp;<br />
        <table border="0" cellspacing="0" cellpadding="0" width="190" id="leftcol">

       <tr>
       <td width="10"></td>
        <td width="170" class="smallText"><br />
            <p><span class="subHeader">Photog #1</span><br />
            Mary Sue - Mother, she prefers her Canon camera. She still uses film, but has the photos put onto digital cd when developed. </p>

            <p><span class="subHeader">Photog #2</span><br />
            Bobby - Hip son, he prefers his HP kid digital camera. Not that old, but still takes interesting pictures.</p>

            <p><span class="subHeader">Photog #3</span><br />
            Jenny - Cool Teen, she has a Sony digital video camera. She takes movies and still photos. Just don't expect her to smile too much.</p>

             <br />
            &nbsp;<br />          </td>
         <td width="10">&nbsp;</td>
        </tr>
      </table>  </td>
    <td width="100%">&nbsp;</td>
  </tr>
  <tr>
    <td width="165">&nbsp;</td>
    <td width="50">&nbsp;</td>
    <td width="167">&nbsp;</td>
    <td width="138">&nbsp;</td>
    <td width="50">&nbsp;</td>
    <td width="190">&nbsp;</td>
    <td width="100%">&nbsp;</td>
  </tr>
</table>
</body>
</html>

the problem is not the MIME type, but it's in the JS script (if it's the one at 'http://orca.st.usm.edu/~tbonnet2/440/p5/family.js') 问题不是MIME类型,而是在JS脚本中(如果是'http://orca.st.usm.edu/~tbonnet2/440/p5/family.js'中的那个)

A closing parentheses is missing, look into closeMenu(): 缺少右括号,请查看closeMenu():

function closeMenu() {

 /* get the current menu id */
 var ID = this.id + "List"

 /* determine active menu */
 activeMenu = document.getElementById(ID);
 if (activeMenu) {              // <------ THIS CONDITIONAL BLOCK ISN'T CLOSED!
    activeMenu.style.display = "none";
    activeMenu = null;
 /* run the moveItemLeft function by setting a time interval */
 timeID = setInterval("moveItemLeft()", 1);

}

I had a problem with the same description as the author of the question. 我有一个与问题作者相同描述的问题。

But, in my case, the js file was not being re-read from the disk. 但是,就我而言,没有从磁盘重新读取js文件。 Initially, my js file was empty as I began testing the HTML structure. 最初,当我开始测试HTML结构时,我的js文件为空。 Later when I added code to the js file, the browser window, which had been open, did not re-read the js file, even after "refreshing" the browser. 稍后,当我向js文件中添加代码时,即使“刷新”浏览器后,打开的浏览器窗口也不会重新读取js文件。

The js file was there, it was in the same directory, but the content of the file was memory cached in the browser. js文件在那里,它在同一目录中,但是文件的内容存储在浏览器中的内存中。 I saw a message to this effect somewhere in the Google debugger, but I am unable to find that message again. 我在Google调试器中的某处看到了一条消息,表明这种效果,但是我无法再次找到该消息。 The message stated the HTML file was disk cached and the js file was memory cached. 该消息表明HTML文件是磁盘缓存的,而js文件是内存缓存的。

The HTML file was refreshed when I clicked the browser's refresh button, but the js file was not getting refreshed and was still showing empty in the "sources" tab from the initial running of the page before js content was added. 当我单击浏览器的刷新按钮时,HTML文件被刷新,但是js文件没有被刷新,并且在添加js内容之前,页面的初始运行在“源”选项卡中仍然显示为空。

To fix, I closed the browser window, opened a new browser window, and everything started working correctly. 为了解决此问题,我关闭了浏览器窗口,打开了一个新的浏览器窗口,然后一切开始正常运行。 The new window re-read the js script from disk. 新窗口从磁盘重新读取js脚本。

If you dont have any errors like the answer above and are using a web host.... 1. login to control panel and check if file actually exist there. 如果您没有上述答案中的任何错误并且正在使用Web主机。...1.登录控制面板并检查文件是否确实存在。

I ran into the same problem, i was using ftp(winSCP) to upload my files when i made changes and even changed file names i ran into the same problem but when i went to the control panel(file manager) all the changes i was making through the winSCP were not being reflected then i uploaded everything through the control panel it all started working fine. 我遇到了同样的问题,当我进行更改甚至更改了文件名时,我正在使用ftp(winSCP)上传文件,我遇到了同样的问题,但是当我进入控制面板(文件管理器)时,我所做的所有更改通过winSCP制作并没有得到反映,然后我通过控制面板上传了所有内容,一切开始正常运行。

Check with, for example, Firefox's console if there are errors regarding the loading of that js file (and post them). 例如,使用Firefox的控制台检查有关该js文件加载的错误(并发布)。 Maybe, the file is loaded but it contains errors that don't let you see the alert 也许文件已加载,但包含的错误使您无法看到警报

Note that your server returns a different MIME-type than specified in your HTML source: 请注意,您的服务器返回的HTML类型不同于HTML源中指定的MIME类型:

<script type="text/javascript" src="family.js"></script>
$ HEAD http://orca.st.usm.edu/~tbonnet2/440/p5/family.js
200 OK
Connection: close
Date: Thu, 23 Feb 2012 02:22:59 GMT
Accept-Ranges: bytes
ETag: "1320142-9f5-4b9979ec8c280"
Server: Apache/2.2.3 (Red Hat)
Content-Length: 2549
Content-Type: application/x-javascript             # <-- see here
Last-Modified: Thu, 23 Feb 2012 01:32:42 GMT
Client-Date: Thu, 23 Feb 2012 02:22:59 GMT
Client-Peer: 131.95.35.200:80
Client-Response-Num: 1

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

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