我可以使用 <link> 标签加载 javascript 代码吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2631635/
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
Can I load javascript code using <link> tag?
提问by john terry
Can I load javascript code using <link>tag in my website ?
我可以<link>在我的网站中使用标签加载 javascript 代码吗?
For example I have a javascript file, test.js, which contains the simple code alert('hello');
例如,我有一个 javascript 文件test.js,其中包含简单的代码alert('hello');
Can I make the popup window appear using:
我可以使用以下方法显示弹出窗口:
<link href="test.js"></link>
回答by bobince
No. There was a proposal to allow:
不可以。有一项提议允许:
<link rel="script" href=".../script.js"/>
analogously to stylesheets. This is even quoted as an example in the HTML 4 DTD, but browser implementation never happened. Shame, as this would have been much cleaner.
类似于样式表。这甚至在HTML 4 DTD 中被引用为示例,但浏览器实现从未发生过。耻辱,因为这会更干净。
回答by Jonathon Faust
You need to use the <script>tag to include JavaScript source files:
您需要使用该<script>标签来包含 JavaScript 源文件:
<script type="text/javascript" src="mysrc.js"></script>
The end tag must be the full </script>, don't abbreviate the way you can with some tags as in <script type="text/javascript" src="..."/>.
结束标签必须是完整的</script>,不要像<script type="text/javascript" src="..."/>.
Yes, alert statements in the included source will appear when they are evaluated by the browser.
是的,包含的源中的警报语句会在浏览器评估它们时出现。
For information on the uses of the <link>tag, see w3.org.
有关<link>标签使用的信息,请参阅w3.org。
回答by HeyJude
Modern browsers support the preloadkeyword, which is used to preload various resources, including scripts. From MDN:
现代浏览器支持preload关键字,用于预加载各种资源,包括脚本。来自MDN:
The
preloadvalue of the<link>element'srelattribute allows you to write declarative fetch requests in your HTML<head>, specifying resources that your pages will need very soon after loading, which you therefore want to start preloading early in the lifecycle of a page load, before the browser's main rendering machinery kicks in. This ensures that they are made available earlier and are less likely to block the page's first render, leading to performance improvements.A simple example might look like this (see our JS and CSS example source, and also live):
<head> <meta charset="utf-8"> <title>JS and CSS preload example</title> <link rel="preload" href="style.css" as="style"> <link rel="preload" href="main.js" as="script"> <link rel="stylesheet" href="style.css"> </head> <body> <h1>bouncing balls</h1> <canvas></canvas> <script src="main.js"></script> </body>
元素属性的
preload值允许您在 HTML 中编写声明性获取请求,指定页面加载后很快需要的资源,因此您希望在页面加载生命周期的早期,在浏览器的主要渲染之前开始预加载机器开始发挥作用。这确保它们更早可用并且不太可能阻止页面的第一次渲染,从而提高性能。<link>rel<head>一个简单的示例可能如下所示(请参阅我们的JS 和 CSS 示例源代码以及live):
<head> <meta charset="utf-8"> <title>JS and CSS preload example</title> <link rel="preload" href="style.css" as="style"> <link rel="preload" href="main.js" as="script"> <link rel="stylesheet" href="style.css"> </head> <body> <h1>bouncing balls</h1> <canvas></canvas> <script src="main.js"></script> </body>
回答by Jonathan Quimbly
The other option for this is, you can dynamically insert a script file into the current document, by creating a SCRIPT tag, setting its "src" attribute to the URI of the script, and then inserting it as a child of the page's HEAD node.
另一个选项是,您可以通过创建 SCRIPT 标记,将其“src”属性设置为脚本的 URI,然后将其作为页面 HEAD 节点的子节点插入,动态地将脚本文件插入到当前文档中.
Doing those things will get the browser to fetch the script file, load it into the document, and execute it.
执行这些操作将使浏览器获取脚本文件,将其加载到文档中并执行它。
回答by SkItZ
To answer your question directly, no. Not by that method. However I was led to this question while searching a similar issue which lead me to this question. Seeing the answers already supplied which for the most part are correct I went to check syntax on http://w3schools.com/. It seems that with HTML5 there is a new attribute for for the script elements in html.
直接回答你的问题,不。不是用那个方法。然而,我在搜索一个类似的问题时被引导到了这个问题,这导致了我这个问题。看到已经提供的答案大部分是正确的,我去http://w3schools.com/检查语法。似乎在 HTML5 中,对于 html 中的脚本元素有一个新属性。
This new attribute allows javascript files to be defered or loaded and executed asynchronously (not to be confused with AJAX).
这个新属性允许 javascript 文件被延迟或异步加载和执行(不要与 AJAX 混淆)。
I'm just going to leave the link here and let you read up on the details yourself as it is already supplied on the internet.
我只想把链接留在这里,让你自己阅读细节,因为它已经在互联网上提供了。
回答by Syntactic
JavaScript code would generally be loaded using a script tag, like so:
JavaScript 代码通常会使用脚本标签加载,如下所示:
<script type="text/javascript" src="test.js"></script>
回答by Oded
No. A Link tag like is for CSS files or for relationallinks (like next).
不。链接标签之类的用于 CSS 文件或关系链接(如next)。
This is not the way to load javascript into the page. You need to use the <script>tag:
这不是将 javascript 加载到页面中的方法。您需要使用<script>标签:
<script language="javascript" src="file.js" />

