【问题标题】:Create separate js file for inclusion创建单独的 js 文件以包含
【发布时间】:2012-06-13 08:24:35
【问题描述】:

我有一个 html 文件,其中当前包含我的所有 javascript 代码。我想制作一个单独的 .js 文件,将所有 javascript 代码放入其中并将其包含在我的 .html 文件中。我试过这样做,还删除了 .js 文件中的开始/结束标签,并将这一行放在 .html 文件中-

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

但是,这不起作用。关于有什么问题的任何想法? 提前致谢。

----------编辑-------------- ----

<html>  

<title>Process Details</title>  

<head>  
<script type="text/javascript" src="https://www.google.com/jsapi"></script>  
<script type="text/javascript" src="myfile.js">  
</script>  

<style type="text/css">  
  body { font-size: 80%; font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; }  
  ul#tabs { list-style-type: none; margin: 30px 0 0 0; padding: 0 0 0.3em 0; }  
  ul#tabs li { display: inline; }  
  ul#tabs li a { color: #42454a; background-color: #dedbde; border: 1px solid #c9c3ba; border-bottom: none; padding: 0.3em; text-decoration: none; }  
  ul#tabs li a:hover { background-color: #f1f0ee; }  
  ul#tabs li a.selected { color: #000; background-color: #f1f0ee; font-weight: bold; padding: 0.7em 0.3em 0.38em 0.3em; }  
  div.tabContent { border: 1px solid #c9c3ba; padding: 0.5em; background-color: #f1f0ee; }  
  div.tabContent.hide { display: none; }  
  .heading { font-size:110%; font-family:'Century Schoolbook'; color: maroon; }  
  .data { font-size:110%; font-family:'Century Schoolbook'; color:black; }  
</style>  

</head>  

<body onload="init()">
<center>
<font face='Calibri' size='3' color='Blue'>PID : </font>
<select id='list' onchange="refreshData()">
</select>
<form>
<input type="button" id="Refresh" onclick="refreshPage()" value="Refresh Data"/>
</form>
</center>

<ul id="tabs">
<li><a href="#memory">Memory</a></li>
<li><a href="#thread">Thread</a></li>
<li><a href="#summary">Summary</a></li>
</ul>
<div class="tabContent" id="memory">
<table id="graphtab" align="center">

<tr>
<td id="heap" align="center"></td>
<td id="nonheap" align="center"></td>
</tr>
<tr>
<td id="resident" align="center"></td>
<td id="pagefaults" align="center"></td>
</tr>

</table>
</div>
<div class="tabContent" id="thread">
<table id="threadtab" align="center">

<tr>
<td id="cpuusage" align="center"></td>
<td id="count" align="center"></td>
</tr>
<tr>
<td id="threadlist" align="center"></td>
<td id="threaddets" align="center"></td>
</tr>

</table>
</div>
<div class="tabContent" id="summary">
</div>
<a href="client.html">Home Page</a>
</body>

</html>

【问题讨论】:

  • 您与我们共享的代码没有问题。
  • 仍然无法正常工作。页面加载时没有任何反应。
  • 请放纵我们并将整个 html 发布在同一原始问题的编辑部分中。
  • 我很确定,如果我错了,请告诉我,正文上的 onload 指定页面何时加载,而不是外部 javascript 库。因此,您的 init 函数很可能尚未定义,因此它会停止运行脚本(或者如果您希望它运行 init,则根本不运行它)。要让它在页面加载时运行,您可以这样做stackoverflow.com/questions/1235985/… 或使用 jQuery(但这可能是矫枉过正)。
  • @h2ooooooo:我在 Chrome、Firefox 和 IE9 中测试了代码,一切正常。这是 HTML:pastebin.com/UEafWLb4,myfile.js 的内容是整个 jQuery 库,后面跟着 load() pastebin.com/7F0gCdqL 的定义

标签: javascript external-js


【解决方案1】:

使用http 协议而不是https。在 Chrome 而不是 Firefox中使用 https 会引发 GET 异常。

您的&lt;title&gt; 应该在&lt;head&gt; 标记内。

【讨论】:

    猜你喜欢
    • 2010-10-21
    • 2017-12-04
    • 2023-03-25
    • 1970-01-01
    • 2019-08-04
    • 2011-12-25
    • 1970-01-01
    • 2015-10-25
    • 2019-07-25
    相关资源
    最近更新 更多