简体   繁体   中英

How do i implement javascript on my site?

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?

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, eg you cannot do this <script src="..." /> .

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>

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>

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>

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