JavaScript là một trong những ngôn ngữ lập trình được giải thích, nhẹ và được biên dịch phổ biến nhất. Nó là đồng bộ và đơn luồng. Các chương trình trong JavaScript được gọi là tập lệnh được thực thi dưới dạng văn bản thuần túy. Chúng tôi có thể viết các tập lệnh này trực tiếp trên trang HTML của mình hoặc sử dụng tệp JavaScript bên ngoài. JavaScript có thể thực thi trong trình duyệt và cả trên máy chủ hoặc trên thực tế trên bất kỳ thiết bị nào có chương trình đặc biệt gọi là công cụ JavaScript. JavaScript được sử dụng cho phía máy chủ cũng như trong các phát triển phía máy khách
Có 2 cách để sử dụng javascript trong tệp HTML
- Internal JavaScript: JavaScript can be added directly to the HTML file by writing the code inside the tag . We can place the tag either inside or the tag according to the need.
- External JavaScript: The other way is to write JavaScript code in another file having a .js extension and then link the file inside the or tag of the HTML file in which we want to add this code.
Ví dụ. Ví dụ này mô tả việc sử dụng Javascript nội bộ
HTML
Hi Geeks, Welcome to GfG07
________ 108 ________ 109 ________ 110
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG08
Hi Geeks, Welcome to GfG0____110
Hi Geeks, Welcome to GfG2____108____04
Hi Geeks, Welcome to GfG5
Hi Geeks, Welcome to GfG4
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG8
Hi Geeks, Welcome to GfG0
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG08
3____110
Hi Geeks, Welcome to GfG2______108______77
8
7
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG2____108____133
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG35
Hi Geeks, Welcome to GfG2
Hi Geeks, Welcome to GfG37
Hi Geeks, Welcome to GfG2____139
Hi Geeks, Welcome to GfG2____08
Hi Geeks, Welcome to GfG33
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG8
3
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG8
Hi Geeks, Welcome to GfG09
Hi Geeks, Welcome to GfG10
đầu ra
Hi Geeks, Welcome to GfG
Ví dụ. Ví dụ này mô tả việc sử dụng Javascript bên ngoài
HTML
Hi Geeks, Welcome to GfG07
________ 108 ________ 109 ________ 110
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG08
Hi Geeks, Welcome to GfG0____110
Hi Geeks, Welcome to GfG2______108
Hi Geeks, Welcome to GfG4
Hi Geeks, Welcome to GfG092
Hi Geeks, Welcome to GfG4
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG8
Hi Geeks, Welcome to GfG0
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG08
3____110
Hi Geeks, Welcome to GfG2______108______77
8
7
Hi Geeks, Welcome to GfG107
Hi Geeks, Welcome to GfG35
Hi Geeks, Welcome to GfG2______108
Hi Geeks, Welcome to GfG33
Hi Geeks, Welcome to GfG112
Hi Geeks, Welcome to GfG113
Hi Geeks, Welcome to GfG114
Hi Geeks, Welcome to GfG115
Hi Geeks, Welcome to GfG33
Hi Geeks, Welcome to GfG117
Hi Geeks, Welcome to GfG8
3
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG8
Hi Geeks, Welcome to GfG09
Hi Geeks, Welcome to GfG10
tệp bên ngoài. GfG. js
Javascript
________ 1085 ________ 1086 ________ 1087
đầu ra.
Hi Geeks, Welcome to GfG
JavaScript bên ngoài là gì?
External JavaScript is when the JavaScript Code[script] is written in another file having an extension .js and then we link this file inside the or tag of our HTML file in which the code is to be added. The use of external JavaScript is more practical when the same code is to be used in many different web pages. Using an external script is easy , just put the name of the script file[our .js file] in the src [source] attribute of tag. External JavaScript file can not contain tags.
cú pháp.
Giá trị thuộc tính
- loại. Nó được sử dụng để chỉ định loại tập lệnh MIME và xác định nội dung của Thẻ. Nó có một giá trị mặc định là “ văn bản/javascript” .
- src. Nó được sử dụng để chỉ định URL của an tệp JavaScript bên ngoài.
- không đồng bộ. Nó là một thuộc tính boolean. Khi xuất hiện, nó chỉ định rằng tập lệnh sẽ được thực thi không đồng bộ khi có sẵn.
- trì hoãn. Đó là một thuộc tính boolean that được sử dụng để chỉ định rằng tập lệnh được thực thi khi trang đã hoàn tất quá trình phân tích cú pháp.
- chính trực. Nó được sử dụng để cấp quyền cho Trình duyệt kiểm tra tập lệnh đã tìm nạp để đảm bảo mã nguồn không bao giờ được tải.
- chính sách giới thiệu. Nó được sử dụng để chỉ định thông tin tham chiếu sẽ được gửi đến máy chủ khi tìm nạp tập lệnh.
Multiple script files can also be added to one page using several tags.
Hi Geeks, Welcome to GfG3
Ví dụ. Ví dụ này mô tả việc sử dụng các tệp Javascript bên ngoài trong đó một con trỏ di chuyển văn bản cụ thể sau đó văn bản thay đổi thành một văn bản khác
HTML
Hi Geeks, Welcome to GfG07
________ 108 ________ 109 ________ 110
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG08
Hi Geeks, Welcome to GfG0____110
Hi Geeks, Welcome to GfG2______108
Hi Geeks, Welcome to GfG4
Hi Geeks, Welcome to GfG09
Hi Geeks, Welcome to GfG4
Hi Geeks, Welcome to GfG10
_______02____108____014
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG2
Hi Geeks, Welcome to GfG17
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG19
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG21
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG23
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG25
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG27
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG29
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG31
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG33
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG35
Hi Geeks, Welcome to GfG18
Hi Geeks, Welcome to GfG37
Hi Geeks, Welcome to GfG2
Hi Geeks, Welcome to GfG39
Hi Geeks, Welcome to GfG2____08
Hi Geeks, Welcome to GfG14
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG8
Hi Geeks, Welcome to GfG0
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG08
3____110
Hi Geeks, Welcome to GfG2______108
7
Hi Geeks, Welcome to GfG54
Hi Geeks, Welcome to GfG113
Hi Geeks, Welcome to GfG56
8
7
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG2______108
Hi Geeks, Welcome to GfG33
Hi Geeks, Welcome to GfG112
Hi Geeks, Welcome to GfG113
Hi Geeks, Welcome to GfG114
Hi Geeks, Welcome to GfG115
Hi Geeks, Welcome to GfG33
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG8
3
Hi Geeks, Welcome to GfG10
Hi Geeks, Welcome to GfG11
Hi Geeks, Welcome to GfG8
Hi Geeks, Welcome to GfG09
Hi Geeks, Welcome to GfG10
mã JS
GfG. js
________ 076 ________ 077 ________ 1087
Hi Geeks, Welcome to GfG79
Hi Geeks, Welcome to GfG80
Hi Geeks, Welcome to GfG81
Hi Geeks, Welcome to GfG82
Hi Geeks, Welcome to GfG83
Hi Geeks, Welcome to GfG84
Hi Geeks, Welcome to GfG85
Hi Geeks, Welcome to GfG086
Hi Geeks, Welcome to GfG87
Hi Geeks, Welcome to GfG88
đầu ra. Khi con trỏ đã di chuột vào phần tử, văn bản bên trong phần tử [i. e GeekforGeeks] thay đổi thành “Xin chào các chuyên viên máy tính, Chào mừng đến với GfG”.