简体   繁体   English

使用JavaScript代码而不是引用文件?

[英]Using JavaScript Code Instead Of Referencing A File?

EDIT: Sorry, I should have been more clear. 编辑:对不起,我应该更清楚。 My webpage needs to be XHTML compliant. 我的网页需要符合XHTML标准。

I use a hosted blogging platform, and there's no way for me to host a JavaScript file on it. 我使用托管的博客平台,我无法在其上托管JavaScript文件。 We normally reference JavaScript file on a web page like this: 我们通常在网页上引用JavaScript文件,如下所示:

<script type='text/javascript' src='http://example.com/js/mycode.js'></script>

The question is, can I directly reference code in the web page, instead of the file? 问题是,我可以直接引用网页中的代码而不是文件吗? If so, how do I do that? 如果是这样,我该怎么做?

  1. Just paste the JavaScript code in the file between <script> tags? 只需将JavaScript代码粘贴到<script>标记之间的文件中即可?

     <script type='text/javascript'> PASTE THE CODE FROM THE JS FILE HERE </script> 
  2. OR like this? 或者像这样?

     <script type='text/javascript'> //<![CDATA[ PASTE THE CODE FROM THE JS FILE HERE //]]> </script> 

Which of the above two methods is correct? 以上两种方法中哪一项是正确的? If not, is there a better way to do it? 如果没有,有没有更好的方法呢?


Also, can a text (.txt) file be referenced inside a script tag, like this? 此外,可以在脚本标记内引用文本(.txt)文件,如下所示?

<script type='text/javascript' src='http://example.com/js/mycode.txt'></script>

or will there be any issues if I do it like this? 或者如果我这样做会有任何问题吗?

Looking for a knowledgeable answer. 寻找知识渊博的答案。

Can I directly reference code in the web page, instead of the file? 我可以直接在网页中引用代码而不是文件吗?

Yes you can paste your code between the <script> tags. 是的,您可以在<script>标记之间粘贴代码。 HTML even allows bare script tags since browsers ignore the language attribute and type attribute can be omitted according to spec . HTML甚至允许裸脚本标签,因为浏览器忽略language属性,并且可以根据规范省略 type属性。

The type attribute gives the language of the script or format of the data. type属性提供脚本的语言或数据的格式。 If the attribute is present, its value must be a valid MIME type. 如果该属性存在,则其值必须是有效的MIME类型。 The charset parameter must not be specified. 不得指定charset参数。 The default, which is used if the attribute is absent, is "text/javascript". 如果属性不存在,则使用默认值“text / javascript”。

So you can have this: 所以你可以这样:

<script>
    //your code
</script>

Also, you have the option to host your script externally and have this instead 此外,您可以选择在外部托管您的脚本并改为使用此脚本

<script src="http://externalhost.com/yourscript.js"></script>

Also, can a text (.txt) file be referenced inside a script tag, like this? 此外,可以在脚本标记内引用文本(.txt)文件,如下所示?

As far as I know, you can put anything plain text as your source and the browser will read it as JavaScript by default. 据我所知, 您可以将任何纯文本作为源,浏览器默认将其作为JavaScript读取。 Even text from pastebin can act like a script. 即使来自pastebin的文本也可以像脚本一样。

can I directly reference code in the web page, instead of the file? 我可以直接引用网页中的代码而不是文件吗?

your first method should work just fine. 你的第一个方法应该工作得很好。

Also, can a text (.txt) file be referenced inside a script tag 此外,可以在脚本标记内引用文本(.txt)文件

while this may work, but i think you should not go this way. 虽然这可能有用,但我认为你不应该这样做。

In HTML5, just use 在HTML5中,只需使用

<script>
// code from file
</script>

The CDATA tags are no longer necessary (see this older SO post: When is a CDATA section necessary within a script tag? ). 不再需要CDATA标签(请参阅此较旧的SO帖子: 脚本标签中何时需要CDATA部分? )。 Like @meager mentioned, you can paste whatever you need to use inside script tags. 像@meager提到的那样,你可以在脚本标签中粘贴你需要使用的任何内容。 Just remember that any code you write that relies on the contents of that file is either beneath it inside the same script tag, or in its own script tag further down the page. 请记住,您编写的依赖于该文件内容的任何代码都位于相同脚本标记内的代码下方,或者位于页面下方的脚本标记中。

<script>
// file content
// your code that references the file content
</script>

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

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