Home Web Front-end JS Tutorial Detailed introduction to the use of require.js

Detailed introduction to the use of require.js

Dec 30, 2017 pm 04:22 PM
javascript Instructions

We know that RequireJS is a very small javascriptmodule loadingframework, which is the most standardized AMD (Asynchronous Module Definition, asynchronous module loading mechanism) One of the better implementations. The latest version of requireJS is only 14k compressed, which is very lightweight. It can also work in coordination with other frameworks. Using requireJS will definitely improve the quality of our front-end code.

First of all, let’s take a look at a normal page js loading

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="js/index02.js" ></script>
  </head>
  <body>
    <h1>this is a page.</h1>
  </body>
</html>
Copy after login

At this time, if we do not operate the pop-up box, the page will not continue to load, and there will be no page content. This is not the result we want to achieve.

Below we use require.js to perform the operation:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script src="js/require2.1.11.js"></script>
    <script type="text/javascript">
      require(["js/index","js/index01"],function(){
        console.log("当js加载成功后会执行的函数");
      },function(){
        console.log("当js加载失败后会执行的函数");
      });
    </script>
  </head>
  <body>
  </body>
</html>
Copy after login

index.js

define(function(){
  console.log("this is a test!");
  function test(){
    console.log("haha,i am a test!");
  }
  test();
});
Copy after login

I believe you have mastered the method after reading the above introduction, please come for more exciting information Follow php Chinese websiteOther related articles!

Related reading:

What should I do if Google Chrome does not support CSS settings for text smaller than 12px?

Implementation steps of using memcached and xcache for PHP cache optimization

How to implement AJAX and JSONP with native JS

The above is the detailed content of Detailed introduction to the use of require.js. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use DirectX repair tool? Detailed usage of DirectX repair tool How to use DirectX repair tool? Detailed usage of DirectX repair tool Mar 15, 2024 am 08:31 AM

How to use DirectX repair tool? Detailed usage of DirectX repair tool

Introduction to HTTP 525 status code: explore its definition and application Introduction to HTTP 525 status code: explore its definition and application Feb 18, 2024 pm 10:12 PM

Introduction to HTTP 525 status code: explore its definition and application

How to use Baidu Netdisk-How to use Baidu Netdisk How to use Baidu Netdisk-How to use Baidu Netdisk Mar 04, 2024 pm 09:28 PM

How to use Baidu Netdisk-How to use Baidu Netdisk

Learn to copy and paste quickly Learn to copy and paste quickly Feb 18, 2024 pm 03:25 PM

Learn to copy and paste quickly

What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool? What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool? Mar 18, 2024 am 11:07 AM

What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool?

What is PyCharm? Function introduction and detailed explanation of usage What is PyCharm? Function introduction and detailed explanation of usage Feb 20, 2024 am 09:21 AM

What is PyCharm? Function introduction and detailed explanation of usage

How to use Xiaoma win7 activation tool - How to use Xiaoma win7 activation tool How to use Xiaoma win7 activation tool - How to use Xiaoma win7 activation tool Mar 04, 2024 pm 06:16 PM

How to use Xiaoma win7 activation tool - How to use Xiaoma win7 activation tool

How to merge cells using shortcut keys How to merge cells using shortcut keys Feb 26, 2024 am 10:27 AM

How to merge cells using shortcut keys

See all articles