Using JavaScript code instead of a file link?

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

I use a hosted blogging platform and there is no way to host a JavaScript file on it. We usually refer to the JavaScript file on the web page as follows:

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

The question is, can I directly link to the code on the web page, not the file? If so, how to do it?

  • Just paste the JavaScript code into the file between the tags <script>

    <script type='text/javascript'>
    PASTE THE CODE FROM THE JS FILE HERE
    </script>
    
  • OR how is it?

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

Which of these two methods is correct? If not, is there a better way to do this?


Also, is it possible to link to a text file (.txt) inside the script tag, for example:

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

or will there be any problems if I do it like this?

Looking for a helpful answer.

+3
4

-, ?

, <script>. HTML script, language type .

type script . , MIME. charset . , , , "text/javascript".

, :

<script>
    //your code
</script>

, script

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

, (.txt) script, :

, - , JavaScript. pastebin script.

+3

-, ?

.

, (.txt) script

, , .

+2

HTML5

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

CDATA tags are no longer needed (see this older SO link: When is the CDATA section needed in a script tag? ). As @meager mentioned, you can insert whatever you need into use inside script tags. Just remember that any code you write that relies on the contents of this file is located beneath it inside a single script tag or in your script tag further down the page.

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

All Articles