-3

Ok, newbie question. I've written some javascript that I believe will work for my site. Total experience so far, codecademy.com.

Do I just need to save a text file with a .js extension on the end? Then do the whole tag? Do I need a compiler?

  • You're likely to have problems with your JavaScript. Pay attention to your browser's debugging tools. – mason Apr 16 '14 at 02:46
  • @mason you believe that with such question the OP knows anything about debugging tools? – Roko C. Buljan Apr 16 '14 at 02:46
  • @RokoC.Buljan No, but the mere fact that I mentioned them should clue him in to check them out. – mason Apr 16 '14 at 02:50
  • I am aware of debugging tools, though not experienced with any in particular. Let's be honest - any site I upload to, with the description I gave of my level of experience, is hardly going to matter. Who cares if break my practice site? I'll start over. Anyway, downvotes, it's the internet. Haters gonna hate. – atownholditdown Apr 16 '14 at 03:40

4 Answers4

2

JavaScript in web pages is not compiled because it was originally intended to be delivered as part of the HTML document in text form.

Today there are two ways to include JavaScript in a page, the original way was to include it inside of a script tag like so:

<script>

// Your code here

</script>

The preferred method now is to include it as a separate .js file (just a text file with a .js extension) like this:

<script src="/scripts/your-script.js"></script>

Now note a couple of things here:

  • First off, the src attribute is the only one you need. You will often see type and language in other people's code, but this is unnecessary when you are referencing a file because the browser will look at the response from the server to determine what kind of code you are using.
  • The path here is relative to the HTML document's location. Prefixing it with / takes you to the application root.
  • You should get in the habit of placing this tag as close to the bottom of your document as possible because this will allow the page to be displayed faster.
  • The script tag must have an end tag, e.g. you cannot do this <script src="..." />.
Paul
  • 5,992
  • 36
  • 61
  • JavaScript is not compiled? – Roko C. Buljan Apr 16 '14 at 02:52
  • Not in web pages, it is transmitted in plain text. It certainly can be (and of course once it reaches the client it will be), but the OP is asking about including it in a web page. – Paul Apr 16 '14 at 02:55
0

Two ways you should learn how to do it ...

One, link to external file

<script type="text/javascript" src="pathtofile.js"></script>

be sure you have a path to that file. If the file is in the same folder as your html file, the name is all you need, otherwise you'll need more path info

src="folder/subfolder/file.js"

or

src="http://domain.tld/folder/file.js"

Two, wrap the code in the script tag

<script type="text/javascript">
    // your code here    
</script>
bsoist
  • 745
  • 3
  • 10
  • If I remove `type="text/javascript"` would the browser think it's something else than JS? – Roko C. Buljan Apr 16 '14 at 02:47
  • In my experience, no, but I have always included it. – bsoist Apr 16 '14 at 02:48
  • If you are including script on the page you should use application/javascript, otherwise it is not necessary because the Content-Type response header from the .js request will be used instead. – Paul Apr 16 '14 at 03:00
  • 2
    When I started using JavaScript many years ago, there were a lot of other possibilities. I understand it may not be necessary now, but I don't think being unambiguous is a bad thing. If you don't want to use it, don't. I really think it's not a big deal. If we're being picky, there are still other scripts you could embed, but you are right, if the code is JS, the attribute isn't necessary. – bsoist Apr 16 '14 at 03:05
0

In most circumstances, JavaScript is not compiled.

There's two man ways of running JavaScript. You can embed directly in an HTML page.

<script type="text/javascript">
//your JavaScript goes here
</script>

Or you can put the JavaScript in a file (usually you end the filename with .js) and then add a reference to it on your page.

<script type="text/javascript" src="MyCode.js"></script>
mason
  • 28,517
  • 9
  • 66
  • 106
  • `type="text/javascript"` ... what else could it be? No need to use it. – Roko C. Buljan Apr 16 '14 at 02:46
  • My IDE yells at me if I don't put `text/javascript`. And anyways, you could have server side code or something else, like in an ASP.NET situation. – mason Apr 16 '14 at 02:49
  • Your IDE (name?) could be out of date same as jsFiddle yells if you don't put a closing tag in `` or `` tag (HTML5 !!). In an .NET environment, is there something else you could put inside a ` – Roko C. Buljan Apr 16 '14 at 02:54
  • From the curators of ECMAScript we're asked to omit that property cause not needed. – Roko C. Buljan Apr 16 '14 at 02:56
  • @RokoC.Buljan Yes, you can embed C# on a page which executes on the server side. Putting `text/javascript` removes ambiguity and doesn't hurt. With Silverlight, you can also put IronPython scripts in there. Anyways, you're muddling up the question. – mason Apr 16 '14 at 02:57
  • Wow! I really did not know about IronPython inside ` – Roko C. Buljan Apr 16 '14 at 03:35
0

I'll paste an example here so you can picture by yourself...
You could also include the resource using the other answers approach

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>My Fisrt JS</title>
    </head>
    <body>
        <h1>Nice heading title here</h1>
        <section>
            <article>You could write something amazing here</article>
        </section>
        <!-- Remember to insert your scripts in the bottom, right before closing body tag -->
        <script>
            // Your javascript code goes here, inside a script tag
            var myCoolVar = 'ABCD';
            var myReverseCoolVar = myCoolVar.split('').reverse().join('');

            console.log(myReverseCoolVar);
        </script>
    </body>
</html>
recamilio
  • 89
  • 4
  • You code does not tell the thing most people fight with: "to put `script` after `` or right before `` tag. You could improve a bit (a lot) your answer." – Roko C. Buljan Apr 16 '14 at 02:50
  • @RokoC.Buljan He put it right after body. And in any case, there are plenty of reason to put it in the head if wanted. Please don't make things more complex than they need to be. – mason Apr 16 '14 at 02:51
  • @mason ok than I have the divine duty to suggest to put any *script* as close to the `

    ` tag :)

    – Roko C. Buljan Apr 16 '14 at 02:58
  • @RokoC.Buljan Please don't act like your advice is divine. There are reasons to place JavaScript in other locations, such as the the head. – mason Apr 16 '14 at 03:07
  • `

    ` is the very next line. He couldn't have put it any closer.

    – bsoist Apr 16 '14 at 03:07
  • @bsoist exactly, so is `` ;) – Roko C. Buljan Apr 16 '14 at 03:10
  • @RokoC.Buljan okay, so he followed your advice on both counts - no discussion necessary – bsoist Apr 16 '14 at 03:14
  • @mason Not divine in a religious way, but useful. There are scarse reasons to put JS right in `head`. Neither do search engines like it. – Roko C. Buljan Apr 16 '14 at 03:15
  • @bsoist I'm not trying to put a dot on anything. I was just saying that to a person who know almost *nothing* (OP) about JS, this question as any other could be much more improved... I'm just too tired to write my own, so still expecting to upvote someone. (BTW imagine yourself at the beginnings, seeing the code above would you put HTML before or after ` – Roko C. Buljan Apr 16 '14 at 03:19
  • 1
    I don't have to imagine, because I was a beginner once. When I started, scripts were supposed to go in the head. Things are different now, and you are right, putting them at the bottom is widely recommended. I guess I just read all your comments as badgering people about details instead of helpful. If you were trying to help, great. I should also note that while I agree with your advice that scripts should go as near the bottom as possible as a good habit to get into, I do think that advice is overstated by many. In many cases, it makes very little difference. – bsoist Apr 16 '14 at 03:26
  • @bsoist http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element well there's a big difference in my opinion :) my comments might look like smartass, but I don't like to ruin the party (an answer) but rather at first to push for more improvements. So yes. I adore to help and recommend! – Roko C. Buljan Apr 16 '14 at 03:28
  • That link has nothing to do with what I said. We agree - it's a good habit to form, why put the scripts anywhere else - but you know as well as I do that there are other ways to address the problem pointed out in that thread. I'm sure you are also aware the putting the scripts at the bottom is often touted as the way to speed up a page, when sometimes putting more thought into how much you really need to drag into a page in the first place is just as important. – bsoist Apr 16 '14 at 03:35
  • @bsoist exactly! I agree with you – Roko C. Buljan Apr 16 '14 at 03:36
  • Thank you for the feedback. I really appreciate those constructive points. I'll edit the answer to improve it... – recamilio Apr 16 '14 at 03:45