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?
-
Just paste the JavaScript code in the file between
<script>tags?<script type='text/javascript'> PASTE THE CODE FROM THE JS FILE HERE </script> -
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.
Yes you can paste your code between the
<script>tags. HTML even allows bare script tags since browsers ignore thelanguageattribute andtypeattribute can be omitted according to spec.So you can have this:
Also, you have the option to host your script externally and have this instead
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.