3

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.

its_me
  • 10,998
  • 25
  • 82
  • 130
  • **PS:** Since Google Blogger doesn't have an option to host your javascript files, you need to reference the code directly in the page between the ` – its_me Nov 20 '12 at 11:53

4 Answers4

3

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.

Joseph
  • 117,725
  • 30
  • 181
  • 234
  • 1
  • 1
    @Derek [it can be omitted](http://www.w3.org/TR/html5/the-script-element.html#script): *"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"."* – Joseph May 25 '12 at 03:32
  • @JosephtheDreamer So, if I am going to be referencing a text file should it be like this? `` -- is this good? or the browser will still raise alerts in dev console? – its_me May 25 '12 at 03:40
  • @AahanKrish like the spec says, `type` can be optional. And even if `.txt` works, why `.txt`? Can't you rename it to `.js`? If you have problems on your host regarding files, why not host the `.js` files in some other free file hosting service? – Joseph May 25 '12 at 03:42
  • @JosephtheDreamer I want to host the file with Google, and all the tuts online suggest that the only place to try hosting it is Google Sites -- which only allows text files. Anyway, I think I will be going with the direct code between ` – its_me May 25 '12 at 03:46
2

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.

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
  • The external file is not a JavaScript file. It's a text file with JavaScript code. So, can such a text (.txt) file be referenced inside a script tag? – its_me May 25 '12 at 03:25
  • 1
    You don't need CDATA for XHTML compliance, you 'need' it if you want to be able to write code like `for (var i=0;i<10;i++)` instead of `for (var i=0;i<10;i++)` in XHTML. – Phrogz May 25 '12 at 03:26
  • @AahanKrish In practice, yes: you can reference a file over HTTP with a random file extension (including .txt) and sending the wrong mime type (e.g. `text/plain`) and modern browsers will generally warn you in the developer console but just consume the script anyway. – Phrogz May 25 '12 at 03:28
  • have a look at this about CDATA http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag – Ibrahim Azhar Armar May 25 '12 at 03:30
  • 3
    @IbrahimAzharArmar The accepted answer is wrong. See the comment on that answer with 19 upvotes. – Phrogz May 25 '12 at 03:34
0

In HTML5, just use

<script>
// code from file
</script>
user229044
  • 232,980
  • 40
  • 330
  • 338
0

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>
Community
  • 1
  • 1
jonathanatx
  • 1,603
  • 3
  • 12
  • 13
  • Note that a CDATA block _is_ necessary and appropriate in XHTML (if you want to place raw `<` and `&` in your script instead of writing entities `<` and `&` for them) but not HTML. – Phrogz May 25 '12 at 03:25