简体   繁体   English

是否需要外部 .js 文件<script> tags?

[英]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功能。

external.js:外部.js:

<script>
    alert('external')
</script>

Then, external.js is added to index.html .然后, external.js被添加到index.html

index.html:索引.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>标签才可以。

external.js:外部.js:

alert('external')

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

相关问题 <script> require('renderer.js') </script>- 不连接js文件 - <script> require('renderer.js') </script> - does not connect the js file 带脚本标记的外部Javascript文件 - External Javascript File with Script tags 如何使外部js文件写入html <script> tags? Via plain JS or jQuery - How to make an external js file write to the html <script> tags? Via plain JS or jQuery HTML5 脚本标签内的 js 代码有效,但不在外部 js 文件中 - js code inside script tags of HTML5 work but not in external js file 包含外部js文件并在外部js文件中调用函数不适用于相同的脚本标记 - Inclusion of external js file and calling a function inside the external js file does not work with same script tag Greasemonkey脚本不加载外部JS和CSS文件 - Greasemonkey script does not load external JS and CSS file 为什么代码在其中工作<script> tag, but not in external .js file? 🤨 - Why does the code work within <script> tag, but not in external .js file? 🤨 包括来自其他文件的外部脚本标签 - include external script tags from other file Electron.js 如何在 html 脚本标签中使用 require 函数 - Electron.js how to use require function in html script tags 为什么defer属性对不能在Firefox中不提供外部js文件的脚本标签起作用? - Why doesn't the defer attribute work on script tags that don't source an external js file in Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM