1. Javascript là gì?
Javascript là ngôn ngữ lập trình của HTML và Web chạy trên browser dưới dạng các script.
A Document object represents the HTML document that is displayed in that window. The Document object has various properties that refer to other objects which allow access to and modification of document content.
The way a document content is accessed and modified is called the Document Object Model, or DOM. The Objects are organized in a hierarchy. This hierarchical structure applies to the organization of objects in a Web document.
- Window object − Top of the hierarchy. It is the outmost element of the object hierarchy.
The window object represents an open window in a browser. - Document object − Each HTML document that gets loaded into a window becomes a document object. The document contains the contents of the page.
- Form object − Everything enclosed in the <form>...</form> tags sets the form object.
- Form control elements − The form object contains all the elements defined for that object such as text fields, buttons, radio buttons, and checkboxes. They have events, attributes, styles.
Here is a simple hierarchy of a few important objects -
Closures: Một số link tham khảo về Closures là
2. Why Study JavaScript?
JavaScript is one of the 3 languages all web developers must learn:1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScript to program the behavior of web pages
3. Ví dụ 1: JavaScript can change HTML content
The innerHTML property sets or returns the HTML content (inner HTML) of an element.<!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>
</body>
</html>
4. Ví dụ 2: JavaScript can change CSS style
<!DOCTYPE html><html>
<body>
<h1>What Can JavaScript Do?</h1>
<p id="demo">JavaScript can change the style of an HTML element.</p>
<button type="button" onclick="document.getElementById('demo').style.fontSize='35px'">Click Me!</button>
</body>
</html>
5. Ví dụ 3: JavaScript can change HTML attributes
<!DOCTYPE html><html>
<body>
<h1>What Can JavaScript Do?</h1>
<p>JavaScript can change HTML attributes.</p>
<p>In this case JavaScript changes the src (source) attribute of an image.</p>
<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>
<img id="myImage" src="pic_bulboff.gif" style="width:100px">
<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>What Can JavaScript Do?</h1>
<p id="demo">JavaScript can hide HTML elements.</p>
<button type="button" onclick="document.getElementById('demo').style.display='none'">Click Me!</button>
</body>
</html>
Tương tự, để hiển thị phần tử đang ở trạng thái hidden thì dùng đặt style.display='block'
7. Ví dụ 5: JavaScript với sự kiện onchange
<html><head>
<script type="text/javascript">
function updateInputValue(ishVal){
// ID of current element
var elementID = document.getElementById('CurrentUnitId').value;
document.getElementById(elementID).value = ishVal;
}
function updateTextBox(textboxID) {
//var txtID = document.getElementById(textboxID);
var orgID = document.getElementById('organizationSearchName');
orgID.value = '';
document.getElementById('CurrentUnitId').value = textboxID;
var tempElement = document.createElement("markActionURL");
tempElement.type = "hidden";
tempElement.value = '${actionUrl}';
document.getElementById('CommonTagsTreeChooserUnitID').appendChild(tempElement);
openTreeChooserDialog('organizationSearch','${actionUrl}');
}
function updateInput(ish){
document.getElementById("fieldname").value = ish;
}
</script>
</head>
<body>
<input type="text" name="fieldname" id="fieldname" />
<input type="text" name="thingy" onchange="updateInput(this.value)" />
</body>
</html>
this.value là value của phần tử đang xét.
8. Ví dụ 6: JavaScript chạy trưc tiếp một hàm cùng với trang web
Dùng đoạn mã sau nhúng vào body của trang html:<script type="text/javascript">javascript:changeColumnWidthR2();</script>
khi load trang web, browser sẽ gọi hàm changeColumnWidthR2()。
Nội dung của hàm changeColumnWidthR2
<script type="text/javascript">
function changeColumnWidthR2(){
$('#ColumnLabelEmployeeName').attr('style','width: 15%');
$('#ColumnLabelCompany).attr('style','width: 35%');
$('#ColumnLabelPosition').attr('style','width: 15%');
$("#Output_Other_Help").html("<font color='red'><center>Click checkbox để chọn organization.</center></font>");
}
</script>
Ngoài ra, có thể gọi những hàm javascript mà không cần tới prefix javascript:
ví dụ:
<script>
focusTextboxElement("frmSearch");
pressSearchButton();
</script>
9. Điều kiện true/false với 1 biến số
var inputValue = null | undefined | NaN | empty string ("") | 0 | falseif(inputValue)
alert('false');
else
alert('true value');
=> trả về giá trị false cho biến số inputValue.
10. What does this mean: "undefined object property"?
What does this mean: "undefined object property"?
Actually it can mean two quite different things! First, it can mean the property that has never been defined in the object and, second, it can mean the property that has an undefined value. Let's look at this code:
Is o.a undefined? Yes! Its value is undefined. Is o.b undefined? Sure! There is no property 'b' at all! OK, see now how different approaches behave in both situations:
We can clearly see that typeof obj.prop == 'undefined' and obj.prop === undefined are equivalent, and they do not distinguish those different situations. And 'prop' in obj
can detect the situation when a property hasn't been defined at all and
doesn't pay attention to the property value which may be undefined. |
11. JSON - check an Integer variable is valid
if(!(zoneId % 1 === 0)) {isZoneIdValid = false;
result.code = 422;
result.message = "zoneid = " + zoneId + " khong hop le";
}
if(jsonInput.maxItems >= 0)
maxItems = jsonInput.maxItems;
12. JSON - check empty in javascript
- function isEmpty(value){return (typeof value === "undefined" || value === null || value.length === 0 || value === "undefined");
}
- hasOwnProperty
if(jsonInput.hasOwnProperty('maxItems')) {}
Next: javascript-can-ban-2
0 comments:
Post a Comment