javascript 使用外部源在 <script> 标签内执行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3579597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:35:46  来源:igfitidea点击:

Executing code inside a <script> tag with an external source

javascript

提问by devadam4

Why does this snippet:

为什么这个片段:

<script type="text/javascript">
alert("1111");
</script>  

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

result in "1111" being alerted, but this one:

导致“1111”被警告,但这个:

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

alert("111");
</script>

doesn't cause "111" to alert? Is it not possible to put code in the same <script>tag that loads an external script?

不会引起“111”警报?是否不能将代码放在<script>加载外部脚本的同一个标签中?

回答by Fyodor Soikin

Well, this is just how the <script>tag works. If you have a srcattribute, the content of the tag gets ignored.

嗯,这就是<script>标签的工作方式。如果你有一个src属性,标签的内容会被忽略。

Simply use another <script>tag, what's the problem with that?

只需使用另一个<script>标签,那有什么问题呢?

回答by cmullins

The below JavaScript is correct:

下面的 JavaScript 是正确的:

<html>
     <head>
          <script type="text/javascript"> alert("1111"); </script>
          <script type="text/javascript" src="xxx.js"> </script>
     </head> 
     <body>
          <p> The actual script is in an external script file called "xxx.js".</p>
     </body>
</html>

If you only want one script tag then put the

如果你只想要一个脚本标签,那么把

 alert("1111");

inside of the xxx.js file.

在 xxx.js 文件中。

The alert doesn't work when it is placed in between the script tag with a src because that is the way it is intended to work. It ignores anything between the open and closing script tags when src is specified.

当警报放置在带有 src 的脚本标记之间时,警报不起作用,因为这是它的工作方式。当指定 src 时,它会忽略打开和关闭脚本标记之间的任何内容。

回答by Paulie Waulie

Your second example is attempting to reference a script from an external file called xxx.js in this case located in the same folder as the html file. If you created that file and placed the alert into that file and moved your script block into the head tag then you would find it would work.

您的第二个示例试图从名为 xxx.js 的外部文件中引用脚本,在这种情况下,该文件与 html 文件位于同一文件夹中。如果您创建了该文件并将警报放入该文件并将您的脚本块移动到 head 标记中,那么您会发现它会起作用。

Placing javascript in external files is an encouraged practice because it allows you to reuse common functions in many pages with a simple inlcude statement in your html. Plus it keeps your html files much cleaner.

将 javascript 放在外部文件中是一种鼓励的做法,因为它允许您在 html 中使用简单的 inlcude 语句在许多页面中重用常用函数。此外,它还可以让您的 html 文件更干净。

When you start writing lots of javascript you can combine all your script into one file and then minify it using something like JSMin : http://www.crockford.com/javascript/jsmin.html

当您开始编写大量 javascript 时,您可以将所有脚本合并到一个文件中,然后使用 JSMin 之类的工具将其缩小:http://www.crockford.com/javascript/jsmin.html

This compresses all your script into a tiny form that is unreadable to humans but is much quicker for your sites visitors because it means the script file is smaller and there is only one request to serve the file to the client.

这会将您的所有脚本压缩成一种人类无法读取的微小形式,但对您的站点访问者来说却要快得多,因为这意味着脚本文件更小,并且只有一个请求可以将文件提供给客户端。

On a side note, another helpful tool when writing javascript is JSLint.

附带说明一下,编写 javascript 时另一个有用的工具是JSLint

It parses your javascript and informs you of syntax errors and also bad practices.

它会解析您的 javascript 并通知您语法错误和不良做法。

Happy Coding

快乐编码

Paul

保罗