简体   繁体   中英

Using JavaScript Code Instead Of Referencing A File?

EDIT: Sorry, I should have been more clear. My webpage needs to be XHTML compliant.

I use a hosted blogging platform, and there's no way for me to host a JavaScript file on it. We normally reference JavaScript file on a web page like this:

<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?

     <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?

<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. HTML even allows bare script tags since browsers ignore the language attribute and type attribute can be omitted according to spec .

The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "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?

As far as I know, you can put anything plain text as your source and the browser will read it as JavaScript by default. Even text from pastebin can act like a script.

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

while this may work, but i think you should not go this way.

In HTML5, just use

<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? ). Like @meager mentioned, you can paste whatever you need to use inside script tags. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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