Home > Web Front-end > HTML Tutorial > How to introduce js files into html

How to introduce js files into html

醉折花枝作酒筹
Release: 2022-04-29 10:35:11
Original
85579 people have browsed it

Two import methods: 1. Use the src attribute of the script tag to import the js file, the syntax is "". 2. Reference js files in JavaScript code with the syntax "script tag object.src = "file path";".

The operating environment of this tutorial: windows7 system, javascript1.8.5&&HTML5 version, Dell G3 computer.

js file refers to a text file that stores JavaScript code, with the suffix name ".js".

So how to introduce js files in html, there are the following two methods:

The first one: use the src attribute of the script tag to introduce js files

<script type="text/javascript" src="../js/jquery-1.8.3.js"></script>//(相对路径)
<script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script>//(绝对路径)
Copy after login

Second type:Referencing external js in JavaScript code

<script>
  window.onload = function(){
    var script = document.createElement("script");
    script.setAttribute("type","text/javascript");
    script.src = "http://common.cnblogs.com/script/jquery.js";
    document.getElementsByTagName("head")[0].appendChild(script);  
  }
</script>
Copy after login

Recommended learning:html video tutorial

The above is the detailed content of How to introduce js files into html. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template