មេរៀនទី២៣: មេរៀនសង្ខេប Syntax ក្នុង HTML5

ការគាំទ្រ កម្មវិធីរុករក Browser Support: កំណែចុងក្រោយបំផុតរបស់ ក្រុមហ៊ុន Apple Safari , Google Chrome , មាន Mozilla Firefox មាន និង លក្ខណៈពិសេស របស់ Opera ទាំងអស់ HTML5 ការគាំទ្រជាច្រើន ក្នុងការ រុករក តាមប្រព័ន្ធអ៊ីនធីណែតដោយប្រើ HTML5។ កម្មវិធីរុករក បណ្តាញ ទូរស័ព្ទចល័ត ដែល បានដំឡើងនៅលើ iPhones , iPad និង ទូរស័ព្ទ ប្រព័ន្ធប្រតិបត្តិការ Android ទាំងអស់ដែល មានការគាំទ្រល្អប្រសើ ជាមួយ HTML5
HTML5 Syntax HTML ដែលThe DOCTYPE:
HTML 5 ប្រើ
Character Encoding:
The
The tag:
So far you were writing as follows:
HTML5 ពត៌មាន
HTML5 Elements: Following is the example of an HTML5 element:
...HTML5 Attributes:
The markup for an HTM 5 document would look like the following:...
...
...
...
មេរៀន HTML5 Attributes
ខាងក្រោមនេះ
Standard Attributes:...
The attributes listed below are supported by almost all the HTML 5 tags.
Attribute | Options | Function |
accesskey | User Defined | បញ្ជាក់ |
align | right, left, center | តម្រឹម |
background | URL | ដាក់ |
bgcolor | numeric, hexidecimal, RGB values | ដាក់ |
class | User Defined | Classifies ធាតុ |
contenteditable | true, false | បញ្ជាក់ប្រសិនបើ |
contextmenu | Menu id | បញ្ជាក់ |
data-XXXX | User Defined | ឯកសារ HTML ដែល |
draggable | true,false, auto | បញ្ជាក់ថាតើ |
height | Numeric Value | Specifies the height of tables, images, or table cells. |
hidden | hidden | Specifies whether element should be visible or not. |
id | User Defined | Names an element for use with Cascading Style Sheets. |
item | List of elements | ត្រូវបានប្រើដើម្បី |
itemprop | List of items | ធាតុ |
spellcheck | true, false | Specifies if the element must have it’s spelling or grammar checked. |
style | CSS Style sheet | Specifies an inline style for an element. |
subject | User define id | Specifies the element’s corresponding item. |
tabindex | Tab number | បញ្ជាក់លំដាប់ |
title | User Defined | “Pop-up” title for your elements. |
valign | top, middle, bottom | បញ្ឈរ |
width | Numeric Value | Specifies the width of tables, images, or table cells. |
...មេរៀន HTML5 SVG
Embeding SVG in HTML5
HTML5 allows embeding SVG directly using tag which has following simple syntax:
...HTML5 – SVG Circle រង្វង់
Following is the HTML5 version of an SVG example which would draw a cricle using
SVG
HTML5 SVG Circle
This would produce following result in HTML5 enabled latest version of Firefox.

HTML5 – SVG Rectangle ចតុកោណកែង
Following is the HTML5 version of an SVG example which would draw a rectangle using
SVG
HTML5 SVG Rectangle
This would produce following result in HTML5 enabled latest version of Firefox.

Following is the HTML5 version of an SVG example which would draw a line using
SVG
HTML5 SVG Line
This would produce following result in HTML5 enabled latest version of Firefox. style="stroke:red;stroke-width:2"/>

Following is the HTML5 version of an SVG example which would draw an ellipse using
SVG
HTML5 SVG Ellipse
This would produce following result in HTML5 enabled latest version of Firefox.

Following is the HTML5 version of an SVG example which would draw a polygon using
SVG
HTML5 SVG Polygon
This would produce following result in HTML5 enabled latest version of Firefox.

Following is the HTML5 version of an SVG example which would draw a polyline using
SVG
HTML5 SVG Polyline
This would produce following result in HTML5 enabled latest version of Firefox.

ខាងក្រោមនេះ
Similar way you can use
SVG
HTML5 SVG Gradient Ellipse
fx="50%" fy="50%">
his would produce following result in HTML5 enabled latest version of Firefox. style="fill:url(#gradient)" />

វាក្យសម្ព័ន្ធ HTML របស់ HTML5 អនុញ្ញាតឱ្យសម្រាប់ធាតុ MathML ត្រូវបានប្រើនៅខាងក្នុងឯកសារដោយប្រើ
MathML Examples:
Following is a valid HTML5 document with MathML:
Pythagorean theorem
a 2
+
b 2
=
c 2
This would produce following result:
Using MathML Characters:
Consider, following is the markup which makes use of the characters :
MathML Examples
x
2
+
4
x
+
4
=
0

Matrix Presentation Examples:
Consider the following example which would be used to represent a simple 2×2 matrix:
MathML Examples
A
=
x
y
z
w

មេរៀន HTML5 – Web Storage
HTML5 ណែនាំ ស្រដៀងទៅនឹង HTTP សម្រាប់រក្សាទុកទិន្នន័យដែលមាន រចនាសម្ព័ន្ធនៅ ផ្នែកខាងម៉ាស៊ីននិងដើម្បីការពារ។ Cookies ត្រូវបានកំណត់ទៅប្រមាណ 4 គីឡូបៃនៃទិន្នន័យ។
Session Storage:
Following is the code which would set a session variable and access that variable:
if( sessionStorage.hits ){
sessionStorage.hits = Number(sessionStorage.hits) +1;
}else{
sessionStorage.hits = 1;
}
document.write("Total Hits :" + sessionStorage.hits );
Refresh the page to increase number of hits.
Close the window and open it again and check the result.Local Storage:
Following is the code which would set a local storage variable and access that variable every time this page is accessed, even next time when you open the window:
Refresh the page to increase number of hits. Close the window and open it again and check the result. |
Delete Web Storage:
Following is the code which would clear complete local storage:
Refreshing the page would not to increase hit counter. Close the window and open it again and check the result. |
មេរៀន HTML5 – Web SQL Database
មូលដ្ឋានទិន្នន័យ SQL API គឺមិនមែនជាពិតជាផ្នែកមួយនៃការបញ្ជាក់ HTML5 នោះទេប៉ុន្តែវាគឺជាការបញ្ជាក់ដាច់ដោយឡែកដែលបានណែនាំសំណុំនៃ APIs ដើម្បីរៀបចំមូលដ្ឋានទិន្នន័យផ្នែកខាងម៉ាស៊ីន client ដោយប្រើ SQL ។
Opening Database:
The openDatabase method takes care of opening a database if it already exists, this method will create it if it already does not exist.
ដើម្បីបង្កើត
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);Executing queries: To execute a query you use the database.transaction() function. This function needs a single argument, which is a function that takes care of actually executing the query as follows:
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
});INERT Operation:
To create enteries into the table we add simple SQL query in the above example as follows:
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');
});We can pass dynamic values while creating entering as follows:
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
tx.executeSql('INSERT INTO LOGS
(id,log) VALUES (?, ?'), [e_id, e_log];
});READ Operation:
To read already existing records we use a callback to capture the results as follows:
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');
});
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
var len = results.rows.length, i;
msg = "Found rows: " + len + "
";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++){
alert(results.rows.item(i).log );
}
}, null);
});Final Example:
So finally, let us keep this example in full fledged HTML5 document as follows and try to run it with Safari browser.
|
Status Message
This would produce following result with latest version of either Safari or Opera:
Log message created and row inserted.Found rows: 2foobar logmsg |
កម្មវិធីបណ្ដាញសាមញ្ញបង្កើតព្រឹត្តិការណ៍ដែលត្រូវបានបញ្ជូនទៅកាន់ម៉ាស៊ីនបម្រើបណ្ដាញ។ ឧទាហរណ៍ការចុចលើតំណសាមញ្ញមួយ requests ពីទំព័រថ្មីពីម៉ាស៊ីនមេ។
Web Application for SSE:
ដើម្បីប្រើម៉ាស៊ីន Server-Sent Events ត្រូវបានបញ្ជូនព្រឹត្តិការណ៍និងសកម្មភាពនៅក្នុងកម្មវិធី បណ្តាញមួយដែលអ្នកនឹងត្រូវការដើម្បីបន្ថែមធាតុមួយ
/* Define event handling logic here */
Server Side Script for SSE:
A server side script should send Content-type header specifying the type text/event-stream as follows.
print "Content-Type: text/event-stream\n\n";Handle Server-Sent Events:
Let us modify our web application to handle server-sent events. Following is the final example.
|
[TIME]
មេរៀន HTML5 WebSockets
WebSockets គឺជា
var Socket = new WebSocket(url, [protocal] );WebSocket Events:
Following are the events associated with WebSocket object. Assuming we created Socket object as mentioned above:
Event
|
Event Handler
|
Description
|
open | Socket.onopen | This event occurs when socket connection is established. |
message | Socket.onmessage | This event occurs when client receives data from server. |
error | Socket.onerror | This event occurs when there is any error in communication. |
close | Socket.onclose | This event occurs when connection is closed. |
Following are the methods associated with WebSocket object. Assuming we created Socket object as mentioned above:
Method
|
Description
|
Socket.send() | The send(data) method transmits data using the connection. |
Socket.close() | The close() method would be used to terminate any existing connection. |
At the time of writing this tutorial, there are only few web browsers supporting WebSocket() interface. You can try following example with latest version of Chrome, Mozilla, Opera and Safari.
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://localhost:9998/echo");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
alert("Message is received...");
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
Run WebSocketមេរៀន HTML5 – Canvas
You can easily find that
var canvas = document.getElementById(“mycanvas”); |
|
The Rendering Context:
ollowing is the code to get required context along with a check if your browser supports
var canvas = document.getElementById(“mycanvas”);if (canvas.getContext){var ctx = canvas.getContext(’2d’);
// drawing code here } else { // canvas-unsupported code here } |
The latest versions of Firefox, Safari, Chrome and Opera all support for HTML5 Canvas but IE8 does not support canvas natively.
មេរៀន HTML5 – Audio & Video
លក្ខណៈពិសេស HTML5, រួមបញ្ចូលអូឌីយ៉ូ និងការគាំទ្រវីដេអូដោយមិនចាំបាច់ប្រើ Flash។
Embedding Video:
Here is the simplest form of embedding a video file in your webpage:
Your browser does not support the |
Embedding Audio:
HTML5 supports
Your browser does not support the
មេរៀន HTML5 – Geolocation
HTML5 Geolocation API អនុញ្ញាតឱ្យអ្នកចែករំលែកទីតាំងរបស់អ្នកជាមួយនឹងតំបន់ បណ្ដាញចំណូលចិត្តរបស់អ្នក។ ដើម្បីអាចចាប់យក latitude និង longitude របស់អ្នកនិង ត្រូវបានផ្ញើទៅម៉ាស៊ីនមេ តាមបណ្ដាញ។
var geolocation = navigator.geolocation;Example:
Following is a sample code to use any of the above method:
function getLocation() {var geolocation = navigator.geolocation;geolocation.getCurrentPosition(showLocation, errorHandler); } |
Following is a sample code which makes use of Position object. Here showLocation method is a callback method:
function showLocation( position ) {var latitude = position.coords.latitude;var longitude = position.coords.longitude;
… } |
Microdata អនុញ្ញាតឱ្យអ្នកកំណត់ធាតុផ្ទាល់ខ្លួនរបស់អ្នកហើយចាប់ផ្តើមបង្កប់ លក្ខណៈសម្បត្តិផ្ទាល់ ខ្លួននៅក្នុងទំព័រតំបន់បណ្ដាញរបស់អ្នក។ នៅកម្រិតខ្ពស់មួយ microdata មានក្រុមនៃគូឈ្មោះតម្លៃមួយ។
Here there are two items, each of which has the property “name”:
My name is Zara.
My name is Nuha. |
![]() |
My birthday is:
|
Microdata API support: If a browser supports the HTML5 microdata API, there will be a getItems() function on the global document object. If browser doesn’t support microdata, the getItems() function will be undefined.
function supports_microdata_api() {return !!document.getItems;} |
Andy Runie![]() My Blog |
Drag and Drop (DnD) គឺជាអ្នកប្រើគំនិតដ៏មានអនុភាពចំណុចប្រទាក់ដែលធ្វើឱ្យវាងាយស្រួលក្នុងការចម្លង reorder និងការលុបធាតុដោយមានជំនួយពីការចុចកណ្តុរ។ នេះអនុញ្ញាតឱ្យអ្នកប្រើដើម្បីចុច និង សង្កត់ប៊ូតុងកណ្ដុរចុះពីលើធាតុ មួយអូសវាទៅទីតាំងមួយផ្សេងទៀតនិង លែងប៊ូតុងកណ្ដុរ ក្នុងការទម្លាក់ធាតុនៅទីនោះ។
The DataTransfer Object:
The event listener methods for all the drag and drop events accept Event object which has a readonly attribute called dataTransfer. The event.dataTransfer returns DataTransfer object associated with the event as follows:
function EnterHandler(event) {DataTransfer dt = event.dataTransfer;…………. } |
Following are the steps to be carried out to implement Drag and Drop operation:
Step 1: Making an Object Draggable:
Following is the example to make an object dragable:
|
Drag and drop HTML5 demo
Try to drag the purple box around.
ondragstart=”return dragStart(event)”>
Drag Me
Drag Me
Dustbin
Step 2: Dropping the Object:
Following is the example to drop an object into another object:
|
Drag and drop HTML5 demo
Try to move the purple box into the pink box.
ondragstart=”return dragStart(event)”>
Drag Me
Drag Me
ondrop=”return dragDrop(event)”
ondragover=”return dragOver(event)”>Dustbin
ondragover=”return dragOver(event)”>Dustbin
មេរៀន HTML5 Web Workers
ការអនុញ្ញាត JavaScript ត្រូវបានគេរចនាឡើងដើម្បីរត់ក្នុងបរិស្ថានតែមួយ-ខ្សែស្រឡាយមានន័យស្គ្រីបច្រើនមិនអាចរត់
នៅពេលដំណាលគ្នា។ ចូរគិតពីស្ថានភាព មួយដែលជាកន្លែងដែលអ្នកត្រូវការដើម្បីដោះស្រាយព្រឹត្តិការណ៍ចំណុចប្រទាក់: សំណួរនិងបរិមាណដ៏ធំនៃដំណើរការទិន្នន័យ API និងការរៀបចំរបស់ DOM បាន។
Big for loop
function bigLoop(){
for (var i = 0; i <= 10000000000; i += 1){
var j = i;
}
alert("Completed " + j + "iterations" );
}
function sayHello(){
alert("Hello sir...." );
}

Big for loop
var worker = new Worker('bigLoop.js');
worker.onmessage = function (event) {
alert("Completed " + event.data + "iterations" );
};
function sayHello(){
alert("Hello sir...." );
}
Stopping Web Workers: Web Workers don’t stop by themselves but the page that started them can stop them by calling terminate() method.
worker.terminate(); |
|
Checking for Browser Support: Following is the syntax to detect a Web Worker feature support available in a browser:
Checking for Browser Support for web workers
Post a Comment