简体   繁体   English

我如何在我的网站上实现 javascript?

[英]How do i implement javascript on my site?

Ok, newbie question.好的,新手问题。 I've written some javascript that I believe will work for my site.我已经编写了一些我认为适用于我的网站的 javascript。 Total experience so far, codecademy.com.到目前为止的全部经验,codecademy.com。

Do I just need to save a text file with a .js extension on the end?我是否只需要在最后保存一个扩展名为 .js 的文本文件? 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.网页中的 JavaScript 未被编译,因为它最初旨在作为文本形式的 HTML 文档的一部分提供。

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:今天有两种方法可以在页面中包含 JavaScript,最初的方法是将它包含在脚本标签中,如下所示:

<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:现在首选的方法是将其作为单独的 .js 文件(只是带有 .js 扩展名的文本文件)包含,如下所示:

<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.首先, src属性是您唯一需要的。 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.您经常会在其他人的代码中看到typelanguage ,但是当您引用文件时这是不必要的,因为浏览器会查看来自服务器的响应以确定您使用的是哪种代码。
  • The path here is relative to the HTML document's location.此处的路径相对于 HTML 文档的位置。 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="..." /> .脚本标签必须有一个结束标签,例如你不能这样做<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如果该文件与您的 html 文件在同一文件夹中,则该名称就是您所需要的,否则您将需要更多路径信息

src="folder/subfolder/file.js"

or或者

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

Two, wrap the code in the script tag二、将代码包裹在script标签中

<script type="text/javascript">
    // your code here    
</script>

In most circumstances, JavaScript is not compiled.在大多数情况下,不会编译 JavaScript。

There's two man ways of running JavaScript.有两种运行 JavaScript 的方法。 You can embed directly in an HTML page.您可以直接嵌入到 HTML 页面中。

<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.或者您可以将 JavaScript 放在一个文件中(通常您以.js结尾文件名),然后在您的页面上添加对它的引用。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM