[英]Does an external .js file require <script> tags?
外部 .js 文件是否需要内部和包含标签才能工作?
No they're not needed, in fact you'll get a syntax error if you include them.不,它们不是必需的,事实上,如果包含它们,则会出现语法错误。
Your .js
files should contain only JavaScript, no HTML tags around it, like would have inside a page.你的.js
文件应该只包含 JavaScript,周围没有 HTML 标签,就像页面内部一样。
No, it does not.不,不是的。 The external file just needs the code.外部文件只需要代码。
Then you just need to do <script src="file.js" type="text/javascript"></script>
.然后你只需要做<script src="file.js" type="text/javascript"></script>
。
外部 Javascript 不应包含标签。
No, <script>
tags are not needed otherwise error occurs.不,不需要<script>
标签,否则会发生错误。
For example, external.js
has alert
function with <script>
tags.例如, external.js
具有带有<script>
标签的alert
功能。
<script>
alert('external')
</script>
Then, external.js
is added to index.html
.然后, external.js
被添加到index.html
。
<script type="text/javascript" src="external.js"></script>
Then, error occurs.然后,发生错误。 (I used google chrome
) (我使用google chrome
)
Uncaught SyntaxError: Unexpected token '<'
So, only alert
function is fine without <script>
tags.因此,只有alert
功能没有<script>
标签才可以。
alert('external')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.