google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Cơ bản về Dojo trong xây dựng cấu trúc chương trình JavaScript Dojo là một trong những thư viện JavaScript tương tự như jQuery, MooTools,... nhưng ít phổ biến hơn, mặc dù vẫn có cùng các chức năng cơ bản. Đây cũng là một thư viện JavaScript rất dễ cài đặt và giúp các nhà phát triển web lập trình JavaScript tốt và nhanh hơn.

Bài viết này sẽ hướng dẫn nhanh sử dụng Dojo cho những bạn chưa có khái niệm về thư viện JavaScript này.


Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

You use Javascript to animate your website, but your code lacks of structure. Your JavaScript functions are scattered in <script> tags placed all around and you would like to clean this up? Then Dojo is for you.

Installing Dojo

The installation of Dojo is done in three steps: download, inclusion on the site and loading Dojo when the opening the site.

Download Dojo

The best way to get Dojo is to go directly to download the latest stable version from the official website: http://dojotoolkit.org/download/

Installing Dojo

Once downloaded, you will have three folders: "dojo", "dijit" and "dojox" (the latter is in fact an extension of the basic framework).

Copy the three folders to the root of your website (or in a subfolder).

Loading Dojo when the opening the site

Last step, the most important: ask the user's browser to load the framework when loading the site. For this, simply place the following in the "head": a tag source in the "head"

<head>    
   <script src=\"../../dojo/dojo.js\" type=\"text/javascript\"></script>    
</head> 

Where "dojo.js" is the main page of the Javascript framework. Simply edit the path to the dojo folder according to your current installation!

That's it! Dojo is now ready for use on your site!

Using Dojo

How to use the framework?

The addOnLoad method

The dojo.addOnLoad() method: This method is called when the site us loaded. To end the session simply use the dojo.addOnUnload() method, upon closing the website.

Simply place in the home page of your site a JS script calling this method:

<script type="text/javascript">    
   dojo.addOnLoad(    
      function() {    
       // Place the code to be launch when site is loaded here   
      }    
   );    
</script>

The "Application" class

Here below, you will learn how to create a Javascript class via Dojo, using the "Application" class.

Start by creating a "Application.js" file.

To illustrate its use, let's pretend that this class is at the root of the site, in the "mysite" folder, and in the "javascript" subfolder: /mysite/javascript/

Ceating a JS class:

dojo.provide('mysite.javascript.Application');    

dojo.declare('mysite.javascript.Application', null, {    
   myFirstArgument: null,    
   mySecondArgument: null,    

   constructor: function(param1, param2) {    
      myFirstArgument = param1;    
      // Class constructor code.    
   },    

   method1: function(param){    
   },    

   method2: function(){    
   }    
});

Here we see several things:

  • The dojo.provide function is called, as parameter we used the path to the class file. This allows Dojo "know" that the Application class exists.
  • The dojo.declare function is called, in which the code for the Application class is found. This method takes three parameters:
    • the classpath
    • a boolean
    • the content of the class, namely:
      • the attributes of classes, initialized to "null" and followed by a comma.
      • the first method: the constructor, which may or may not have parameters.
      • The other class method (optional).
    • Finally braces and closing parentheses of the method stated.

Each attribute and method is followed by a comma to separate it from other, except the last.

Contents of the Application class

As example:

dojo.provide ('monsite.javascript.Application');
dojo.declare ('monsite.javascript.Application', null, {
   _user: null,
     
   constructor: function (userParam) {
      this._utilisateur = userParam;
   }

   start: function () {
      alert (this._utilisateur);
   }
});

Specifically, here we will create the Application class with a string as parameter, pass the parameter attribute, and then call a method "start ()" which will display this string.

Calling the Class

Back to dojo.addOnLoad. Here we will create an instance of our application class and display a specific string.

The code is as follows:

<script type="text/javascript">    
   dojo.addOnLoad(    
      function() {    
         dojo.require('monsite.javascript.Application');    
         var appli = new monsite.javascript.Application("Salut CommentCaMarche");    
         appli.start();    
      }    
   );    
</script> 

The steps are:

  • Call the dojo.require() method
  • Creating an instance of the Application class using a constructor.
  • Call the start() method of the Application class, which therefore responsible for displaying the previous string.
iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web