简体   繁体   English

如何包括外部JavaScript函数

[英]How to include external javascript functions

I am trying to refer a external javascript file by calling a function in that file as 我试图通过调用该文件中的函数来引用外部javascript文件

<script type="text/javascript" src="external.js">
display('hell0');
</script>

But this code is not working. 但是此代码不起作用。 If i refer the file in separdate script tag it works fine. 如果我在单独的脚本标记中引用该文件,则效果很好。

    <script type="text/javascript" src="external.js"></script>
   <script type="text/javascript">  
    display('hell0');
    </script>

Why the first case is not working? 为什么第一种情况不起作用?

If you use the src attribute to specify an external javascript file to be included you cannot have contents to this script tag. 如果使用src属性指定要包含的外部javascript文件,则此script标签不能包含内容。 The second case is the correct way. 第二种情况是正确的方法。

Quote from the specification : 引用规范

The script may be defined within the contents of the SCRIPT element or in an external file. 该脚本可以在SCRIPT元素的内容内或在外部文件中定义。 If the src attribute is not set, user agents must interpret the contents of the element as the script. 如果未设置src属性,则用户代理必须将元素的内容解释为脚本。 If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI 如果src具有URI值,则用户代理必须忽略元素的内容,并通过URI检索脚本

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM