• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Getting Started with JavaScript in the Traditional Coding Approach
2
3> **NOTE**<br/>For best possible results, use [DevEco Studio V3.0.0.900 Beta3](https://developer.harmonyos.com/cn/develop/deveco-studio#download_beta_openharmony) for your development.
4
5
6## Creating a JavaScript Project
7
81. In DevEco Studio, if no project is open, click **Create Project**; if a project is already open, choose **File** &gt; **New** &gt; **Create Project**. Then, select **Empty Ability** and click **Next**.
9
10   ![en-us_image_0000001223558814](figures/en-us_image_0000001223558814.png)
11
122. On the project configuration page, set **UI Syntax** to **JS** and retain the default values for other parameters.
13
14   ![en-us_image_0000001223877162](figures/en-us_image_0000001223877162.png)
15
163. Click **Finish**. DevEco Studio will automatically generate the sample code and resources that match your project type. Wait until the project is created.
17
18
19## JavaScript Project Files
20
21- **entry**: OpenHarmony project module, which can be built into an ability package ([HAP](../../glossary.md#hap)).
22  - **src &gt; main &gt; js**: a collection of JS source code.
23  - **src &gt; main &gt; js &gt; MainAbility**: entry to your application/service.
24  - **src &gt; main &gt; js &gt; MainAbility &gt; i18n**: resources in different languages, for example, UI strings and image paths.
25  - **src &gt; main &gt; js &gt; MainAbility &gt; pages**: pages contained in **MainAbility**.
26  - **src &gt; main &gt; js &gt; MainAbility &gt; app.js**: ability lifecycle file.
27  - **src &gt; main &gt; resources**: a collection of resource files used by your application/service, such as graphics, multimedia, character strings, and layout files.
28  - **src &gt; main &gt; config.json**: module configuration file. This file describes the global configuration information of the application/service, the device-specific configuration information, and the configuration information of the HAP file.
29  - **build-profile.json5**: current module information and build configuration options, including **buildOption** and **targets**.
30  - **hvigorfile.js**: module-level compilation and build task script. You can customize related tasks and code implementation.
31- **build-profile.json5**: application-level configuration information, including the signature and product configuration.
32- **hvigorfile.js**: application-level compilation and build task script.
33
34
35## Building the First Page
36
371. Use the **Text** component.
38
39   After the project synchronization is complete, choose **entry** &gt; **src** &gt; **main** &gt; **js** &gt; **MainAbility** &gt; **pages** &gt; **index** in the **Project** window and open the **index.hml** file. You can see that the file contains a **&lt;Text&gt;** component. The sample code in the **index.hml** file is shown below:
40
41
42   ```html
43   <!--index.hml-->
44   <div class="container">
45       <text class="title">
46           Hello World
47       </text>
48   </div>
49   ```
50
512. Add a button and bind the **onclick** method to this button.
52
53   On the default page, add an **&lt;input&gt;** component of the button type to respond to user clicks and implement redirection to another page. The sample code in the **index.hml** file is shown below:
54
55
56   ```html
57   <!--index.hml-->
58   <div class="container">
59       <text class="title">
60           Hello World
61       </text>
62
63   <!-- Add a button, set its value to Next, and bind the onclick method to the button. -->
64       <input class="btn" type="button" value="Next" onclick="onclick"></input>
65   </div>
66   ```
67
683. Set the page style in the **index.css** file.
69
70   From the **Project** window, choose **entry** &gt; **src** &gt; **main** &gt; **js** &gt; **MainAbility** &gt; **pages** &gt; **index**, open the **index.css** file, and set the page styles, such as the width, height, font size, and spacing. The sample code in the **index.css** file is shown below:
71
72
73   ```css
74   .container {
75       display: flex;
76       flex-direction: column;
77       justify-content: center;
78       align-items: center;
79       left: 0px;
80       top: 0px;
81       width: 100%;
82       height: 100%;
83   }
84
85   .title {
86       font-size: 100px;
87       font-weight: bold;
88       text-align: center;
89       width: 100%;
90       margin: 10px;
91   }
92
93   .btn {
94       font-size: 60px;
95       font-weight: bold;
96       text-align: center;
97       width: 40%;
98       height: 5%;
99       margin-top: 20px;
100   }
101   ```
102
1034. On the toolbar in the upper right corner of the editing window, click **Previewer** to open the Previewer.
104
105   Below is how the first page looks on the Previewer.
106
107   ![en-us_image_0000001216084724](figures/en-us_image_0000001216084724.png)
108
109
110## Building the Second Page
111
1121. Create the second page.
113
114   In the **Project** window, choose **entry** &gt; **src** &gt; **main** &gt; **js** &gt; **MainAbility**, right-click the **pages** folder, choose **New** &gt; **Page**, name the page **second**, and click **Finish**. Below, you can see the structure of the **second** folder.
115
116![en-us_image_0000001223877210](figures/en-us_image_0000001223877210.png)
117
1182. Add **&lt;Text&gt;** and **&lt;Button&gt;** components.
119
120   Add **&lt;Text&gt;** and **&lt;Button&gt;** components and set their styles, as you do for the first page. The sample code in the **second.hml** file is shown below:
121
122
123   ```html
124   <!--second.hml-->
125   <div class="container">
126       <text class="title">
127           Hi there
128       </text>
129
130   <!-- Add a button, set the value to Back, and bind the back method to the button.-->
131       <input class="btn" type="button" value="Back" onclick="back"></input>
132   </div>
133   ```
134
1353. Set the page style in the **second.css** file. The sample code in the **second.css** file is shown below:
136
137   ```css
138   .container {
139       display: flex;
140       flex-direction: column;
141       justify-content: center;
142       align-items: center;
143       left: 0px;
144       top: 0px;
145       width: 100%;
146       height: 100%;
147   }
148
149   .title {
150       font-size: 100px;
151       font-weight: bold;
152       text-align: center;
153       width: 100%;
154       margin: 10px;
155   }
156
157   .btn {
158       font-size: 60px;
159       font-weight: bold;
160       text-align: center;
161       width: 40%;
162       height: 5%;
163       margin-top: 20px;
164   }
165   ```
166
167
168## Implementing Page Redirection
169
170You can implement page redirection through the [page router](../ui/ui-js-building-ui-routes.md), which finds the target page based on the page URL. Import the **router** module and then perform the steps below:
171
1721. Implement redirection from the first page to the second page.
173
174   In the **index.js** file of the first page, bind the **onclick** method to the button so that clicking the button redirects the user to the second page. The sample code in the **index.js** file is shown below:
175
176
177   ```js
178   // index.js
179   import router from '@ohos.router';
180
181   export default {
182       onclick: function () {
183           router.push({
184               url: "pages/second/second"
185           })
186       }
187   }
188   ```
189
1902. Implement redirection from the second page to the first page.
191
192   In the **second.ets** file of the second page, bind the **back** method to the **Back** button so that clicking the button redirects the user back to the first page. The sample code in the **second.js** file is shown below:
193
194
195   ```js
196   // second.js
197   import router from '@ohos.router';
198
199   export default {
200       back: function () {
201           router.back()
202       }
203   }
204   ```
205
2063. Open any file in the **index** folder and click ![en-us_image_0000001262339067](figures/en-us_image_0000001262339067.png) in the Previewer to refresh the file. The figure below shows the effect.
207
208   ![en-us_image_0000001216269940](figures/en-us_image_0000001216269940.png)
209
210
211## Running the Application on a Real Device
212
2131. Connect the development board running the OpenHarmony standard system to the computer.
214
2152. Choose **File** &gt; **Project Structure** &gt; **Project** &gt; **Signing Configs**, select **Automatically generate signing**, wait until the automatic signing is complete, and click **OK**, as shown below.
216
217   ![en-us_image_0000001223557290](figures/en-us_image_0000001223557290.png)
218
2193. On the toolbar in the upper right corner of the editing window, click ![en-us_image_0000001217047316](figures/en-us_image_0000001217047316.png). The display effect is shown in the figure below.
220
221   ![en-us_image_0000001217527892](figures/en-us_image_0000001217527892.png)
222
223Congratulations! You have finished developing your OpenHarmony application in JavaScript in the traditional coding approach. To learn more about OpenHarmony, see [OpenHarmony Overview](../application-dev-guide.md).
224