Free Hosting

วันจันทร์ที่ 13 มกราคม พ.ศ. 2563

วิธีการสร้างหน้าเว็บให้หน่วงเวลาก่อนจะไปยังหน้าเว็บจริง ๆ ด้วยฟังชั่น header()

0 ความคิดเห็น
สำหรับใครที่ใครที่พัฒนาเว็บไซต์ด้วย php ก็คงจะต้องเคยเห็นเว็บที่มีการหน่วงเวลาก่อนที่จพาเราไปยังหน้าเว็บที่ต้องการซึ่งถ้าเป็นภาษา php แล้วละก็สามารถทำได้ง่าย ๆ ด้วยฟังชั่นที่ชื่อว่า header() ซึ่งฟังชั่นนี้สามารถทำได้หลายรูปแบบไม่ว่าจะเป็น การย้ายหน้าเพจไปยังเว็บอื่น หรือ จะส่งผลออกเป็นไฟล์ ตามสกุลต่าง ๆ ก็ได้หรือจะกำหนัดให้ refresh ตามจำนวนที่ต้องการก็ย่อมได้และบทความนี้ก็จะมาแสดงวิธีการที่จะทำให้หน้าเว็บของเราหน่วงเวลาตามที่เรากำหนดกันจะเป็นอย่างไรลองไปดูเลย

โค้ดที่จะใช่ในการหน่วงเวลาเป็นแบบนี้
Code: Select all
header("Refresh: เวลาเป็นวินาที; url=เพจปลายทาง");

ตัวอย่าง

โค้ดนี้อยู่ในไฟล์ test39.php
Code: Select all
<?php
echo "จะย้ายไปยังหน้า google ภายใน 4 วิ";
header("Refresh: 4; url=https://www.google.com/");
exit();
?>
เมือเราเข้าไปที่หน้า test39.php นี้
Selection_999(2364).png
Selection_999(2364).png (9.87 KiB) Viewed 23 times
เมื่อผ่านไป 4 วิหน้าเว็บก็จะเป็นถูก Refresh เป็นหน้า google
Selection_999(2365).png
Selection_999(2365).png (22.81 KiB) Viewed 23 times
เป็นอย่างไรบ้างครับสำหรับวิธีนี้ไม่ยากอย่างที่คิดใช่ไหมครับเท่านี้เราก็สามารถสร้างหน้าที่จะหน่วงเวลาก่อนจะไปยังหน้าที่ผู้ใช้ต้องการได้แล้วก็หวังว่าผู้ที่เข้ามาอ่านนี้จะได้รับความรู้เกี่ยวกับภาษา php มากขึ้นนะครับ
Read full post »

Chapter 2 CSS

0 ความคิดเห็น
This is chapter 2 of the book Cascading Style Sheets, designing for the Web, by Håkon Wium Lie and Bert Bos (2nd edition, 1999, Addison Wesley, ISBN 0-201-59625-3)
As we explained in the previous chapter, HTML elements enable Web page designers to mark up a document as to its structure. The HTML specification lists guidelines on how browsers should display these elements. For example, you can be reasonably sure that the contents of a strong element will be displayed bold-faced. Also, you can pretty much trust that most browsers will display the content of an h1 element using a big font size... at least bigger than the p element and bigger than the h2 element. But beyond trust and hope, you don't have any control over how your text appears.
CSS changes that. CSS puts the designer in the driver's seat. We devote much of the rest of this book to explaining what you can do with CSS. In this chapter, we begin by introducing you to the basics of how to write style sheets and how CSS and HTML work together to describe both the structure and appearance of your document.

Rules and Style Sheets

To start using CSS, you don't even have to write style sheets. Chapter 16 will tell you how to point to existing style sheets on the Web.
There are two ways to create CSSs. You can either use a normal text editor and write the style sheets "by hand," or you can use a dedicated tool - for example a Web page design application - which supports CSS. The dedicated tools allow you to create style sheets without learning the syntax of the CSS language. However, in many cases the designer will want to tweak the style sheet by hand afterwards, so we recommend that you learn to write and edit CSSs by hand. Let's get started!
H1 { color: green }
What you see above is a simple CSS rule that contains one rule. A rule is a statement about one stylistic aspect of one or more elements. A style sheet is a set of one or more rules that apply to an HTML document. The rule above sets the color of all first-level headings (h1). Let's take a quick look at what the visual result of the rule could be:
Figure 2.1
[image]
We will now start dissecting the rule.

Anatomy of a rule

A rule consists of two parts:
  • Selector - the part before the left curly brace
  • Declaration - the part within the curly braces
    [image]
The selector is the link between the HTML document and the style. It specifies what elements are affected by the declaration. The declaration is that part of the rule that sets forth what the effect will be. In the example above, the selector is h1 and the declaration is "color: green." Hence, all h1 elements will be affected by the declaration, that is, they will be turned green. (The color property just affects the foreground text color, there are other properties for background, border, etc.)
The above selector is based on the type of the element: it selects all elements of type "h1." This kind of selector is called type selector. Any HTML element type can be used as a type selector. Type selectors are the simplest kind of selectors. We discuss other kinds of selectors in See CSS selectors. , "CSS selectors."

Anatomy of a declaration

A declaration has two parts separated by a colon:
  • Property - that part before the colon
  • Value - that part after the colon
    [image]
The property is a quality or characteristic that something possesses. In the previous example, it is color. CSS2 (see separate box) defines around 120 properties and we can assign values to all of them.
The value is a precise specification of the property. In the example, it is "green," but it could just as easily be blue, red, yellow, or some other color.
The diagram below shows all ingredients of a rule. The curly braces ({ }) and colon (:) make it possible for the browser to distinguish between the selector, property, and value.
Figure 2.2 Diagram of a rule.
[image]

Grouping selectors and rules

In designing CSS, brevity was a goal. We figured that if we could reduce the size of style sheets, we could enable designers to write and edit style sheets "by hand." Also, short style sheets load faster than longer ones. CSS therefore includes several mechanisms to shorten style sheets by way of grouping selectors and declarations.
For example, consider these three rules:
H1 { font-weight: bold }
H2 { font-weight: bold }
H3 { font-weight: bold }
     
All three rules have exactly the same declaration - they set the font to be bold. (This is done using the font-weight property, which we discuss in See Fonts. .) Since all three declarations are identical, we can group the selectors into a comma-separated list and only list the declaration once, like this:
H1, H2, H3 { font-style: bold }
This rule will produce the same result as the first three.
A selector may have more than one declaration. For example, we could write a style sheet with these two rules:
H1 { color: green }
H1 { text-align: center }
In this case, we set all h1s to be green and to be centered on the canvas. (This is done using the text-align property, discussed in Chapter  5.)
But we can achieve the same effect faster by grouping the declarations that relate to the same selector into a semicolon-separated list, like this:
H1 {
  color: green;
  text-align: center;
}
All declarations must be contained within the pair of curly braces. A semicolon separates the declarations and may - but doesn't have to - also appear at the end of the last declaration. Also, to make your code easier to read, we suggest you place each declaration on its own line, as we did here. (Browsers won't care, they'll just ignore all the extra whitespace and line breaks.)
Now you have the basics of how to create CSS rules and style sheets. However, you're not done yet. In order for the style sheet to have any effect you have to "glue" your style sheet to your HTML document.

"Gluing" Style Sheets to the Document

For any style sheet to affect the HTML document, it must be "glued" to the document. That is, the style sheet and the HTML document must be combined so that they can work together to present the document. This can be done in any of four ways:
  1. Apply the basic, document-wide style sheet for the document by using the style element.
  2. Apply a style sheet to an individual element using the style attribute.
  3. Link an external style sheet to the document using the link element.
  4. Import a style sheet using the CSS @import notation.
In the next section, we discuss the first method: using the style element. We discuss using the style attribute in Chapter 4 , "CSS selectors," and using the link element and the @import notation in Chapter 16 , "External style sheets."

Gluing by using the STYLE element

You can glue the style sheet and the HTML document together by putting the style sheet inside a style element at the top of your document. The style element was introduced in HTML specifically to allow style sheets to be inserted inside HTML documents. Here's a style sheet (shown in bold) glued to a sample document by using the style element. The result is shown in Figure  2.3 .
<HTML>
  <TITLE>Bach's home page</TITLE>
  <STYLE>
    H1, H2 { color: green }
  </STYLE>
  <BODY>
    <H1>Bach's home page</H1>
    <P>Johann Sebastian Bach was a prolific
        composer. Among his works are:
    <UL>
      <LI>the Goldberg Variations
      <LI>the Brandenburg Concertos
      <LI>the Christmas Oratorio
    </UL>
    <H2>Historical perspective</H2>
    <P>Bach composed in what has been referred to as
      the Baroque period.
  </BODY>
</HTML>
Figure 2.3 The result of adding to a style sheet a rule to turn h1s green and then gluing the style sheet to the document using the style elements. (try it)
[image]
Notice that the style element is placed after the title element and before the body element. The title of a document does not show up on the canvas, so it is not affected by CSS styles.
The content of a style element is a style sheet. However, whereas the content of such elements as h1, p, and ul appears on the canvas, the content of a style element does not show on the canvas. Rather, it is the effect of the content of the style element - the style sheet - that appears on the canvas. So you don't see "{ color: green }" displayed on your screen; you see instead two h1 elements colored green. No rules have been added that affect any of the other elements, so those elements appear in the browser's default color.

Browsers and CSS

For an updated overview of available browsers, see the W3C overview page
For CSS to work as described in this book, you must use a CSS-enhanced browser, that is, a browser that supports CSS. A CSS-enhanced browser will recognize the style element as a container for a style sheet and present the document accordingly. Most browsers that are distributed today support CSS, for example Microsoft Internet Explorer 4 (IE4), Netscape Navigator 4 (NS4) and Opera 3.5 (O3.5). Conservative estimates indicate that more than half the people on the Web use a CSS-enhanced browser, and the figures are steadily rising. Chances are that the people you communicate with have CSS-enhanced browsers. If not, give them a reason to upgrade!
The best source for information on how different browsers support CSS is WebReview's charts
Alas, not all CSS implementations are perfect. When you start experi­menting with style sheets, you will soon notice that each browser comes with a set of bugs and limitations. In general, newer browsers behave better than older ones. IE4 and O3.5 are among the best, and Netscape's next offering - code-named Gecko - also promises much improved support for CSS.
Those who don't use CSS-enhanced browsers can still read pages that use style sheets. CSS was carefully designed so that all content should remain visible even if the browser knows nothing about CSS. Some browsers, such as Netscape's Navigator version 2 and 3 don't support style sheets but they know enough about the style element to fully ignore it. Next to supporting style sheets, this is the correct behavior.
However, other browsers that do not know the style element, such as Netscape's Navigator 1 and Microsoft Internet Explorer 2, will ignore the style tags but display the content of the style element. Thus, the user will end up with the style sheet printed on the top of the canvas. At the time of writing, only a few percent of Web users will experience this problem. To avoid this, you can put your style sheet inside an HTML comment, which we discussed in Chapter  1. Because comments don't display on the screen, by placing your style sheet inside an HTML comment, you prevent the oldest browsers from displaying the style element's content. CSS-enhanced browsers are aware of this trick, and will treat the content of the style element as a style sheet.
Recall that HTML comments start with <!-- and end with -->. Here's an excerpt from the previous code example that shows how you write a style sheet in an HTML comment. The comment encloses the style element content only:
<HTML>
  <TITLE>Bach's home page</TITLE>
  <STYLE>
    <!--
      H1 { color: green }
    -->
  </STYLE>
  <BODY>
    ..
  </BODY>
</HTML>
CSS also has its own set of comments that you can use within the style sheet. A CSS comment begins with "/*" and ends with "*/." (Those familiar with the C programming language will recognize these.) CSS rules inside a CSS comment will not have any effect on the presentation of the document.
The browser also needs to be told that you are working with CSS style sheets. CSS is currently the only style sheet language in use with HTML docu­ments and we don't expect this to change. For XML the situation might be different. But just as there is more than one image format (GIF, JPEG and PNG come to mind), there could be more than one style sheet language. So it's a good habit to tell browsers that they are dealing with CSS. (In fact, HTML requires you to.) This is done with the type attribute of the style ­element. The value of type indicates what type of style sheet is being used. For CSS, that value is "text/css." The following is an excerpt from our previous sample document that shows you how you would write this (in combination with the use of the HTML comment):
<HTML>
  <TITLE>Bach's home page</TITLE>
  <STYLE TYPE="text/css">
    <!--
      H1 { color: green }
    -->
  </STYLE>
  <BODY>
    ..
  </BODY>
</HTML> 
When the browser loads a document, it checks to see if it understands the style sheet language. If it does, it will try to read the sheet, otherwise it will ignore it. The type attribute (see Chapter  1 for a discussion on HTML attributes) on the style element is a way to let the browser know which style sheet language is being used. The type attribute must be included.
To make examples easier to read, we have chosen not to wrap style sheets in HTML comments, but we do use the type attribute throughout this book.

Tree structures and inheritance

Recall from Chapter  1 the discussion about HTML representing a document with a tree-like structure and how elements in HTML have children and parents. There are many reasons for having tree-structured documents. For style sheets, there is one very good reason: inheritance. Just as children inherit from their parents, so do HTML elements. Instead of inheriting genes and money, HTML elements inherit stylistic properties.
Let's start by taking a look at the sample document:
<HTML>
  <TITLE>Bach's home page</TITLE>
  <BODY>
    <H1>Bach's home page</H1>
    <P>Johann Sebastian Bach was a
      <STRONG>prolific</STRONG> composer. Among his
        works are:
    <UL>
      <LI>the Goldberg Variations
      <LI>the Brandenburg Concertos
      <LI>the Christmas Oratorio
    </UL>
  </BODY>
</HTML>
The tree structure of this document is:
[image]
Through inheritance, CSS property values set on one element will be transferred down the tree to its descendants. For example, our examples have up to now set the color to be green for h1 and h2 elements. Now, say, you would like to set the same color on all elements in your document. You could do this by listing all element types in the selector:
<STYLE TYPE="text/css">
  H1, H2, P, LI { color: green }
</STYLE>
However, most HTML documents are more complex than our sample docu­ment, and your style sheet would soon get long. There is a better - and shorter - way. Instead of setting the style on each element type, we set it on their common ancestor, the body element:
<STYLE TYPE="text/css">
  BODY { color: green }
</STYLE>
Since other elements inherit properties from the body element, they will all inherit the color green (Figure  2.4 ).
Figure 2.4 The result of inheritance. (try it)
[image]
As you have seen above, inheritance is a transport vehicle that will distribute stylistic properties to descendants of an element. Since the body element is a common ancestor for all visible elements, body is a convenient selector when you want to set stylistic rules for the entire document.

Overriding Inheritance

In the previous example, all elements were given the same color through inheritance. Sometimes, however, children don't look like their parents. Not surprisingly, CSS also accounts for this. Say you would like for h1 elements to be blue while the rest should be green. This is easily expressed in CSS:
<STYLE TYPE="text/css">
  BODY { color: green }
  H1 { color: navy }
</STYLE>
Since h1 is a child element of body (and thereby inherits from body), the two rules in the above style sheet are conflicting. The first one sets the color of the body element - and thereby also the color of h1 through inheritance - while the second one sets the color specifically on the h1 element. Which rule will win? Let's find out:
The reason why the second rule wins is that it is more specific than the first. The first rule is very general - it affects all elements on the canvas. The ­second rule only affects h1 elements in the document and is therefore more specific.
If CSS had been a programming language, the order in which the rules were specified would determine which of them would win. CSS is not a programming language, and in the above example, the order is irrelevant. The result is exactly the same if we use this style sheet:
<STYLE TYPE="text/css">
  H1 { color: navy }
  BODY { color: green }
</STYLE>
CSS has been designed to resolve conflicts between style sheet rules like the one above. Specificity is one aspect of that. You can find the details in Chapter  15 , "Cascading and inheritance."

Properties that don't inherit

As a general rule, properties in CSS inherit from parent to child elements as described in the previous examples. Some properties, however, don't inherit and there is always a good reason why. We will use the background property (described in Chapter 11) as an example of a property that doesn't inherit.
Let's say you want to set a background image for a page. This is a common effect on the Web. In CSS, you can write:
<HTML>
  <TITLE>Bach's home page</TITLE>
  <STYLE TYPE="text/css">
    BODY {
      background: url(texture.gif) white;
      color: black;
    }
  </STYLE>
  <BODY>
    <H1>Bach's <EM>home</EM> page</H1>
    <P>Johann Sebastian Bach was a prolific
      composer.
  </BODY>
</HTML>
The background property has a URL ("texture.gif") that points to a background image as value. When the image is loaded, the canvas looks like:
There are a few noteworthy things in the above example:
  • The background image covers the surface like a wallpaper - also the backgrounds of the h1 and p element have been covered. This is not due to inheritance, but to the fact that unless otherwise set, all backgrounds are transparent. So, since we haven't set the backgrounds of the h1 or p element to something else, the parent element, body, will shine through.
  • In addition to the URL of the image, a color (white) has also been speci­fied as the background. In case the image can't be found, you will see the color instead.
  • The color of the body element has been set to black. To ensure contrast between the text and the background, it is a good habit to always set a color when the background property is set.
So, exactly why doesn't the background property inherit? Visually, the effect of transparency is similar to inheritance: it looks like all elements have the same backgrounds. There are two reasons: first, transparent backgrounds are faster to display (there is nothing to display!) than other ­backgrounds. Second, since background images are aligned relative to the element they belong to, you would otherwise not always end up with a smooth background surface.

Common tasks with CSS

Setting colors and backgrounds - as described above - are among the most common tasks performed by CSS. Other common tasks include setting fonts and white space around elements. This section gives you a guided tour of the most commonly used properties in CSS.

Common tasks: fonts

Let's start with fonts. If you have used desktop publishing applications in the past, you should be able to read this little style sheet:
H1 { font: 36pt serif }
The rule above sets the font for h1 elements. The first part of the value - 36pt - sets the font size to be 36 points. A "point" is an old typographic unit of measurement which has survived into the digital age. In the next chapter we will tell you why you should use the "em" unit instead of "pt" but for now we'll stick to points. The second part of the value - serif - tells the browser to use a font with serifs (the little hooks at the ends of the strokes, Chapter  5 will tell you all about them). The more decorated serif fonts suit Bach's home page well since the modern sans-serif fonts (fonts without serifs) weren't used in his time. Here is the result:
The font property is a shorthand property for setting several other properties at once. By using it, you can shorten your style sheets and set values on all properties it replaces. If you choose to use the expanded version, you would have to set all of these to replace the example above:
H1 {
  font-size: 36pt;
  font-family: serif;
  font-style: normal;
  font-weight: normal;
  font-variant: normal;
  line-height: normal;
}
Sometimes you only want to set one of these. For example, you may want to slant the text in some elements. Here is an example:
UL { font-style: italic }
The font-style property will not change the font size or the font family, it will only slant the existing font. When set on the ul element, the li elements inside will become slanted, since font-style is inherited. Here is the result when applied to the test page you know by now:
Similarly, the font-weight property is used to change the weight - thickness - of the letters. You can further emphasize the list items by setting their ancestor to be bold:
UL {
  font-style: italic;
  font-weight: bold;
}
     
Which yields:
The last properties, font-variant and line-height, haven't been widely supported in browsers up to now and are therefore not as commonly used yet.

Common tasks: margins

Setting space around elements is a basic tool in typography. The headline above this paragraph has space above it and (slightly less) space below it. This paragraph, as printed in the book, has space on the left and (slightly less) on the right. CSS can be used to express how much space there should be around different kinds of elements.
By default, your browser knows quite a bit about how to display the different kinds of elements in HTML. For example, it knows that lists and blockquote elements should be indented to set them apart from the rest of the text. As a designer, you can build on these settings while at the same time provide your own refinements. Let's use the blockquote element as an example. Here's a test document:
<HTML>
  <TITLE>Fredrick the Great meets Bach</TITLE>
  <BODY>
    <P>One evening, just as Fredrick the Great was
      getting his flute ready, and his musicians
      were assembled, an officer brought him a
      list of the strangers who had arrived. With
      his flute in his hand he ran over the list,
      but immediately turned to the assembled
      musicians, and said, with a kind of
      agitation:
    <BLOCKQUOTE>"Gentlemen, old Bach is come."
    </BLOCKQUOTE>
    <P>The flute was now laid aside, and old Bach, who
      had alighted at his son's lodgings, was immediately
      summoned to the Palace.
  </BODY>
</HTML>
The screen-shot below is how a typical HTML browser would display the document:
As you can see, the browser has added space on all sides of the quoted text. In CSS, this space is called "margins" and all elements have margins on all four sides. The properties are called: margin-top, margin-right, margin-bottom, and margin-left. You can change how the blockquote element is displayed by writing a little style sheet:
BLOCKQUOTE {
  margin-top: 1em;
  margin-right: 0em;
  margin-bottom: 1em;
  margin-left: 0em;
  font-style: italic;
}
The "em" unit will be treated in detail in the next chapter, but we can already now reveal its secret: it scales relative to the font size. So, the above example will result in the vertical margins being as high as the font size (1em) of the blockquote, and horizontal margins having zero width. To make sure the quoted text can still be distinguished, it has been given an italic slant. The result is:
Just like font is a shorthand property to set several font-related properties at once, margin is a shorthand property which sets all margin properties. The above example can therefore be written:
BLOCKQUOTE {
  margin: 1em 0em 1em 0em;
  font-style: italic;
}
The first part of the value - 1em - is assigned to margin-top. From there it's clockwise: 0em is assigned to margin-right, 1em is assigned to margin-bottom, and 0em is assigned to margin-left.
With the left margin set to zero, the quoted text needs more styling to set it apart from the rest of the text. Setting font-style to italic helps, and adding a background color further amplifies the quote:
BLOCKQUOTE {
  margin: 1em 0em 1em 0em;
  font-style: italic;
  background: #EDB;
}
The result is:
As expected, the background color behind the quote has changed. Unlike previous examples, the color was specified in red/green/blue (RGB) components. RGB colors are described in detail in Chapter  11 .
One stylistic problem in the example above is that the background color barely covers the quoted text. The space around the quote - the margin area - does not use the element's background color. CSS has another kind of space, called padding, which uses the background color of the element. In other respects the padding properties are like the margin properties: they add space around an element. Let's add some padding to the quote:
BLOCKQUOTE {
  margin: 1em 0em 1em 0em;
  font-style: italic;
  background: #EDB;
  padding: 0.5em;
}
The result of setting the padding is added space between the text and the rectangle that surrounds it:
Notice that the padding property was only given one value (0.5em). Just like the margin property, padding could have taken 4 values which would have been assigned to the top, right, bottom and left padding respectively. However, when the same value is to be set on all sides, listing it once will suffice. This is true both for padding and margin (as well as some other border properties, which are described in See Space around boxes. ).

Common tasks: links

To make it easier for users to browse in hypertext documents, the links should have a style that distinguishes them from normal text. HTML browsers have often underlined hyperlink text. Also, various color schemes have been used to indicate if the user has previously visited the link or not. Since hyperlinks are such a fundamental part of the Web, CSS has special support for styling them. Here's a simple example:
A:link { text-decoration: underline }
The above example specifies that unvisited links should be underlined:
The links are underlined, as we have specified, but they are also blue, which we have not. When authors do not specify all possible styles, browsers use default styles to fill in the gaps. The interaction between author styles, browser default styles and user styles (the user's own preferences) is ­another example of CSS's conflict resolution rules. It is called the cascade (the "C" of CSS). We will discuss the cascade below.
The selector (A:link) deserves special mentioning. You probably ­recognize "A" as being an HTML element, but the last part is new. ":link" is one of several so-called pseudo-classes in CSS. Pseudo-classes are used to give style to elements based on information outside of the document itself. For example, the author of the document can't know if a certain link will be visited or not. Pseudo-classes are described in detail in Chapter  4, and we'll only give a few more examples here:
A:visited { text-decoration: none }
This rule gives style to visited links, just like A:link gave style to unvisited links. Here is a slightly more complex example:
A:link, A:visited { text-decoration: none }
A:hover { background: cyan }
The last rule introduces a new pseudo-class :hover. Assuming the user is moving a pointing device (like a mouse), the specified style will be applied to the element when the user moves the pointer over ("hovers" over) the link. A common effect is to change the background color. Here is what it looks like:
The :hover pseudo-class has an interesting history. It was introduced in CSS2 after the hover effect became popular among JavaScript programmers. The JavaScript solution requires complicated code compared to the CSS pseudo-class and this is an example of CSS picking up effects that have become popular among Web designers.

A word about Cascading

A fundamental feature of CSS is that more than one style sheet can influence the presentation of a document. This feature is known as cascading because the different style sheets are thought of as coming in a series. Cascading is a fundamental feature of CSS, because we realized that any single document could very likely end up with style sheets from multiple sources: the browser, the designer, and possibly the user.
In the last set of examples you saw that the text color of the links turned blue without that being specified in the style sheet. Also, the browser knew how to format blockquote and h1 elements without being told so explicitly. Everything that the browser knows about formatting is stored in the browser's default style sheet and is merged with author and user style sheets when the document is displayed.
We have known for years that designers want to develop their own style sheets. However, we discovered that users, too, want the option of influencing the presentation of their documents. With CSS, they can do this by supplying a personal style sheet that will be merged with the browser's and the designer's style sheets. Any conflicts between the various style sheets are resolved by the browser. Usually, the designer's style sheet will have the strongest claim on the document, followed by the user's, and then the browser's default. However, the user can say that a rule is very import­ant and it will then override any author or browser styles.
We go into details about cascading in Chapter  15, "Cascading and inheritance." Before that, there is much to learn about fonts, space and ­colors.
Read full post »

ขั้นที่ 7: การใส่ style sheet แยกไฟล์

0 ความคิดเห็น
ตอนนี้เรามีไฟล์ HTML ที่มี style sheet ฝังอยู่แต่หาก เวบไซด์ของเรามีเนื้อหาเพิ่มมากขึ้นเราอาจจะต้องการให้หน้าแต่ละหน้าสามารถใช้สไตล์ร่วม กันได้ ซึ่งมีวิธีที่ดีกว่าการก๊อปปี้ style sheet ลงไปในทุกๆหน้า นั่นก็คือหากเราใส่ style sheet แยกไฟล์ไว้ทุกๆหน้าเวบก็สามารถเรียกสไตล์มาใช้ได้โดยตรง
ในการสร้างไฟล์สำหรับ style sheet เราต้องสร้างไฟล์ text เปล่าขึ้นมาอีกหนึ่งไฟล์ คุณสามารถทำได้โดยเลือก “New” จากเมนู File ในโปรแกรมแก้ไขเพื่อสร้างหน้าเปล่าขึ้นมาหนึ่งหน้า (ถ้าคุณใช้โปรแกรม TextEdit อย่าลืมว่าต้องแก้ไขให้เป็นข้อความธรรมดาก่อนโดยเลือกได้จากเมนู Format)
หลังจากนั้นให้ตัดและวางทุกส่วนที่อยู่ใน element <style> จากไฟล์ HTML ลงไปยังหน้าใหม่แต่อย่าก๊อปปี้ตัว element <style> และ </style> เอง เพราะเป็นส่วนที่ใช้กับ HTML ไม่ใช่ CSS ในหน้าใหม่ของโปรแกรมแก้ไข ควรจะมี style sheet ที่ครบสมบูรณ์ดังนี้
body {
  padding-left: 11em;
  font-family: Georgia, "Times New Roman",
        Times, serif;
  color: purple;
  background-color: #d8da3d }
ul.navbar {
  list-style-type: none;
  padding: 0;
  margin: 0;
  position: absolute;
  top: 2em;
  left: 1em;
  width: 9em }
h1 {
  font-family: Helvetica, Geneva, Arial,
        SunSans-Regular, sans-serif }
ul.navbar li {
  background: white;
  margin: 0.5em 0;
  padding: 0.3em;
  border-right: 1em solid black }
ul.navbar a {
  text-decoration: none }
a:link {
  color: blue }
a:visited {
  color: purple }
address {
  margin-top: 1em;
  padding-top: 1em;
  border-top: thin dotted }
ให้เลือก “Save As…” จากเมนู File ต้องแน่ใจว่าได้เซฟไว้อยู่ในส่วนหรือแฟ้มเดียวกันกับไฟล์ mypage.html และให้เซฟ style sheet เป็น “mystyle.css”
ตอนนี้ให้กลับไปยังหน้าที่มีโคด HTML ให้เอาทุกอย่างออกเริ่มตั้งแต่ tag <style> จนถึง tag </style> และแทนที่ด้วย element <link> ดังนี้
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>My first styled page</title>
  <link rel="stylesheet" href="mystyle.css">
</head>

<body>
[etc.]
ส่วนนี้จะเป็นส่วนที่บอกบราวเซอร์ว่า style sheet จะอยู่ในไฟล์ที่ชื่อว่า “mystyle.css” และเนื่องจากไม่ได้มีการบอกถึงส่วนที่เก็บไฟล์ บราวเซอร์จะดูจากส่วน ที่เก็บไฟล์เดียวกันกับไฟล์ HTML
หากคุณเซฟไฟล์ HTML และโหลดใหม่ในบราวเซอร์ คุณอาจจะมองไม่เห็นถึงความแตกต่างเพราะหน้าเวบยังคงมีสไตล์เดิมแต่ตอนนี้สไตล์จะมาก จากไฟล์ที่อยู่ข้างนอก
ผลสุดท้ายจากการใส่สไตล์
ผลสุดท้ายที่ได้
ขั้นตอนต่อไปก็คือการวางทั้งไฟล์ mypage.html และ mystyle.css บนเวบไซด์ของคุณ (ซึ่งคุณอาจจะต้องการแก้ไขก่อนเล็กน้อย) แต่ส่วนวิธีการวางอย่าง ไรนั้นขึ้นอยู่กับผู้ให้บริการอินเตอร์เน็ทของคุณ
Read full post »

ขั้นที่6: การเพิ่มเส้นแบ่งในแนวนอน

0 ความคิดเห็น
ส่วนที่เราจะเพิ่มเป็นส่วนสุดท้ายใน style sheet คือการใส่เส้นแบ่งในแนวนอนเพื่อแยกข้อความออกจากส่วนของลายเซ็นต์ในส่วนท้าย เราจะใช้'border-top' เพื่อเพิ่มเส้นแบ่งแบบจุดในส่วนบนของ element <address>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>My first styled page</title>
  <style type="text/css">
  body {
    padding-left: 11em;
    font-family: Georgia, "Times New Roman",
          Times, serif;
    color: purple;
    background-color: #d8da3d }
  ul.navbar {
    list-style-type: none;
    padding: 0;
    margin: 0;
    position: absolute;
    top: 2em;
    left: 1em;
    width: 9em }
  h1 {
    font-family: Helvetica, Geneva, Arial,
          SunSans-Regular, sans-serif }
  ul.navbar li {
    background: white;
    margin: 0.5em 0;
    padding: 0.3em;
    border-right: 1em solid black }
  ul.navbar a {
    text-decoration: none }
  a:link {
    color: blue }
  a:visited {
    color: purple }
  address {
    margin-top: 1em;
    padding-top: 1em;
    border-top: thin dotted }
  </style>
</head>

<body>
[etc.]
ตอนนี้การใส่สไตล์ของเราก็เสร็จสมบูรณ์ต่อไปเราจะมาดูว่าเราจะแยก style sheet ไว้อีกไฟล์ได้อย่างไรเพื่อที่ว่าหน้าอื่นสามารถใช้สไตล์ร่วมกันได้
Read full post »

ขั้นที่ 5: การใส่สไตล์ให้กับลิงค์

0 ความคิดเห็น
navigation เมนูนั้นยังดูหน้าตาเหมือนรายการมากกว่าเมนู ดังนั้นเราจะใส่สไตล์เพิ่มเข้าไป เราจะเอาจุด(bullet)ที่ใช้แสดงรายการออกไปก่อน และย้ายรายการแต่ละรายการออกไปทางด้านซ้ายในตำแหน่งเดิมที่ใช้แสดงจุดในแต่ละรายการ นอกจากนั้นเราก็จะใส่พื้นหลังสีขาวและสี่เหลี่ยมสีดำให้แต่ละรายการ (ทำไมน่ะหรือ ไม่มีเหตุผลเฉพาะหรอกเพียงแต่ว่าเราสามารถทำได้)
นอกจากนี้เรายังไม่ได้ระบุวว่าสีที่ใช้ลิงค์เป็นสีอะไร ดังนั้นให้เพิ่มลงไปด้วยสีฟ้าใช้สำหรับลิงค์ที่ผู้อ่านยังมองไม่เห็นส่วนสีม่วงใช้สำหรับลิงค์ ที่ผู้อ่านคลิกเข้าไปดูแล้ว
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>My first styled page</title>
  <style type="text/css">
  body {
    padding-left: 11em;
    font-family: Georgia, "Times New Roman",
          Times, serif;
    color: purple;
    background-color: #d8da3d }
  ul.navbar {
    list-style-type: none;
    padding: 0;
    margin: 0;
    position: absolute;
    top: 2em;
    left: 1em;
    width: 9em }
  h1 {
    font-family: Helvetica, Geneva, Arial,
          SunSans-Regular, sans-serif }
  ul.navbar li {
    background: white;
    margin: 0.5em 0;
    padding: 0.3em;
    border-right: 1em solid black }
  ul.navbar a {
    text-decoration: none }
  a:link {
    color: blue }
  a:visited {
    color: purple }
  </style>
</head>

<body>
[etc.]
เตือน! ล่วงหน้า: โดยทั่วไปแล้ว บราวเซอร์จะแสดงลิงค์ด้วยการขีดเส้นใต้และใส่สี โดยปกติสีที่ใช้จะเหมือนกับสีที่ระบุไว้ในที่นี้ คือ สีฟ้าไว้สำหรับลิงค์ไปยังหน้าที่คุณยังไม่ได้เข้าไปดู (หรือที่เคยเข้าไปดูเมื่อนานมาแล้ว) สีม่วงไว้ใช้สำหรับหน้าที่เข้าไปดูมาแล้ว
ใน HTML การสร้างลิงค์ทำได้โดยใช้ element <a> ดังนั้นเวลาที่เรระบุสีเราจะต้องใส่สไตล์ให้กับ “a” เพื่อแยกความแตกต่างระหว่างลิงค์ที่เยี่ยม ชมแล้วกับลิงค์ที่ยังไม่ได้เยี่ยมชม CSS ได้กำหนดให้มี “pseudo-classes” (:link และ :visited) ที่เรียกว่า “pseudo-classes” เพื่อแยกแยะความแตกต่างระหว่าง classattributes, ที่แสดงบน HTML โดยตรงตัวอย่างเช่นclass="navbar" ที่แสดงไว้ในตัวอย่างข้างต้น
Read full post »

ขั้นที่4: การเพิ่ม navigation bar

0 ความคิดเห็น
รายชื่อในส่วนบนของหน้า HTML ตั้งใจจะใช้เป็นเมนู navigation เวบไซด์หลายเวบมีการใช้เมนูวางเรียงไว้ด้านบนหรือไว้ด้านข้างของ หน้าและหน้านี้ก็ควรมีเช่นกันซึ่งเราจะวางไว้ด้านซ้ายเนื่องจากดูแล้วน่าแล้วน่าสนใจกว่าวางไว้ด้านบน
เมนูดังกล่าวนี้มีอยู่ในหน้า HTML แล้วโดยใช้ <ul>แสดงรายการไว้ด้านบนลิงค์ยังไม่สามารถใช้ได้เนื่องเวบไซด์ของเราในขณะนี้มี เพียงหน้าเดียวแต่ในขณะนี้เรื่องนั้นไม่สำคัญ แต่แน่นอนว่าสำหรับเวบไซด์จริงๆแล้วไม่ควรจะมีลิงค์ที่ใช้งานไม่ได้

ดังนั้นเราจะต้องย้ายรายชื่อลงมาอยู่ด้านซ้ายและย้ายข้อความที่เหลือทั้งหมดมาทางด้านขวาเล็กน้อยเพื่อให้มีพื้นที่ CSS propertyที่เราใช้คือ 'padding-left' (สำหรับย้ายข้อความในส่วน body) และ 'position' 'left' และ 'top' (เพื่อย้ายเมนู)
ยังมีวิธีอื่นๆที่สามารถทำได้หากคุณดูในส่วน “column” หรือ “layout” ในหน้า Learning CSS (เรียนรู้ CSS)คุณจะเจอเทมเพลตที่ใช้ได้เลยหลายอันแต่ อันนี้ก็ใช้ได้เหมาะสมกับจุดประสงค์ของเรา
ในหน้าโปรแกรมแก้ไข ให้เพิ่มบรรทัดต่อไปนี้เขาไปในไฟล์ HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>My first styled page</title>
  <style type="text/css">
  body {
    padding-left: 11em;
    font-family: Georgia, "Times New Roman",
          Times, serif;
    color: purple;
    background-color: #d8da3d }
  ul.navbar {
    position: absolute;
    top: 2em;
    left: 1em;
    width: 9em }
  h1 {
    font-family: Helvetica, Geneva, Arial,
          SunSans-Regular, sans-serif }
  </style>
</head>

<body>
[etc.]
หากคุณเซฟไฟล์อีกครั้งและ reload ในบราวเซอร์ทีนี้คุณก็จะได้รายชื่อลิงค์ที่อยู่ทางด้านซ้ายของข้อความหลัก ซึ่งตอนนี้หน้าเวบนี้ก็ดูน่าสนใจขึ้น
ภาพจากหน้าจอที่มีเมนูด้านซ้าย
ข้อความหลักย้ายไปอยู่ที่ด้านขวาและตอนนี้รายชื่อลิงค์ย้ายไปอยู่ที่ด้านซ้ายแล้วแทนที่จะอยู่ด้านบน
เดือน! ล่วงหน้า: การใส่ 'position: absolute' เป็นการบอกว่า element ul ถูกวางไว้แยกออกจากข้อความที่มาก่อนหรือหลังภายในเอกสารและการใช้ 'left' และ 'top' ก็เพื่อแสดงให้รู้ว่าอยู่ในตำแหน่งใด ในกรณีนี้ คือ 2em จากด้านบนและ 1em จากด้านซ้ายของหน้าต่าง
'2em' หมายถึง 2 เท่าของขนาดฟอนท์ปัจจุบันเช่น ถ้าเมนูใช้ฟอนท์ขนาด 12 point ดังนั้น '2em' จะเท่ากับ 24 point 'em' เป็นหน่วยการวัดประเภทหนึ่งที่มีประโยชน์ในการใช้ CSS เนื่องจากสามารถนำมาใช้กับฟอนท์ที่ผู่อ่านมักจะใช้ได้โดยอัตโนมัติ บราวเซอร์ส่วนใหญ่ มีเมนูที่ไว้เพิ่มหรือลดขนาดของฟอนท์คุณจะเห็นได้ว่าขนาดของเมนูจะเพิ่มเมื่อขนาดของฟอนท์เพิ่มซึ่งจะไม่เป็นไปตามนั้นหากในกรณี ที่เราใช้หน่วยเป็นพิกเซลแทน
Read full post »

ขั้นที่ 3: การเพิ่มฟอนท์

0 ความคิดเห็น
อีกสิ่งหนึ่งที่สามารถทำได้อย่างง่ายคือการแยกความแตกต่างบางส่วนให้กับฟอนท์ในแต่ละ element ของหน้า ดังนั้นให้ลองกำหนดข้อความโดยใช้ฟอนท์ “Georgia” ยกเว้น h1 ซึ่งเราจะใช้ฟอนท์ “Helvetica” แทน
เมื่อแสดงบนเวบแล้วเราไม่สามารถมั่นใจได้เลยว่าผู้อ่านจะมีฟอนท์อะไรบ้างในเครื่องคอมพิวเตอร์ของเขาดังนั้นเราควรจะต้องเพิ่มทางเลือก บางอย่างเข้าไปด้วยเช่นกัน ถ้าผู้อ่านไม่มีฟอนท์ Georgia อาจจะใช้ Times New Roman หรือ Times แทนก็ได้และหากไม่มีฟอนท์ ทั้งสองเลยบราวเซอร์จะเลือกฟอนท์ใดๆก็ตามที่อยู่ในตระกูลserifs หากไม่มี Helvetica ก็ใช้ Geneva Arial และ SunSans-Regular ที่มีลักษณะคล้ายกันแทนแต่หากไม่สามารถใช้ได้เลย บราวเซอร์ก็จะเลือกฟอนท์อื่นที่ไม่ใช่ serif
ในโปรแกรมแก้ไขให้เพิ่มบรรทัดต่อไปนี้เข้าไป
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>My first styled page</title>
  <style type="text/css">
  body {
    font-family: Georgia, "Times New Roman",
          Times, serif;
    color: purple;
    background-color: #d8da3d }
  h1 {
    font-family: Helvetica, Geneva, Arial,
          SunSans-Regular, sans-serif }
  </style>
</head>

<body>
[etc.]
หากคุณเซฟไฟล์นี้อีกครั้งและกดปุ่ม “Reload” ในบราวเซอร์ ตอนนี้ฟอนท์ที่ปรากฎควรจะมีหน้าตาที่เปลี่ยนไปทั้งในส่วน heading และ ข้อความอื่นๆ
ภาพจากหน้าจอที่มีการเพิ่มฟอนท์
ตอนนี้ข้อความหลักและ heading ใช้ฟอนท์ที่แตกต่างกัน
Read full post »

ขั้นที่2: การใส่สีเพิ่มเติม

0 ความคิดเห็น
คุณอาจจะเห็นว่ามีข้อความสีดำอยู่บนพื้นหลังสีขาวแต่ทั้งนี้ก็ขึ้นอยู่กับการอ่านค่าของบราวเซอร์ ดังนั้นวิธีที่ง่ายวิธีหนึ่งที่เราสามารถสร้างสไตล์ ให้กับหน้าเวบได้คือการใส่สี (ให้เปิดบราวเซอร์ทิ้งไว้เพราะเราจะต้องใช้อีกภายหลัง)
เราจะเริ่มต้นจาการใส่ style sheet ที่อยู่ในไฟล์เดียวกันกับ HTML ก่อน ภายหลังเราจะแยกไฟล์ HTML กับ CSS ออกจากกัน การแยกไฟล์ นั้นมีข้อดีเนื่องจากเราสามารถนำ style sheet เดียวกันมาใช้กับไฟล์ HTML ได้หลายไฟล์ได้ง่ายโดยที่เขียน style sheet เพียงแค่ครั้งเดียว แต่สำหรับขั้นตอนนี้ เราจะเก็บทุกอย่างไว้ในไฟล์เดียวกัน
เราต้องใส่ element <style>ในไฟล์ HTML style sheet style sheet จะอยู่ภายใน element นี้ ดังนั้นให้กลับไปยังหน้าต่างในโปรแกรม แก้ไขและเพิ่มบรรทัดต่อไปนี้ 5 บรรทัดในส่วน head ของไฟล์ HTML บรรทัดที่ใส่เพิ่มจะเป็นตัวสีแดง
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>My first styled page</title>
  <style type="text/css">
  body {
    color: purple;
    background-color: #d8da3d }
  </style>
</head>

<body>
[etc.]
บรรทัดแรกบอกไว้ว่า style sheet ดังกล่าวเขียนขึ้นใน CSS (“text/css”) บรรทัดที่สองบอกว่าเราได้เพิ่ม style ลงไปใน element “body” บรรทัดที่สามเป็นตัวกำหนดสีของข้อความให้เป็นสีม่วงและบรรทัดถัดไปเป็นตัวกำหนดพื้นหลังให้เป็นสีประมาณเหลืองอมเขียว
Style sheets ใน CSS นั้นสร้างขึ้นมาจากกฎ กฏแต่ละกฏประกอบไปด้วยสามส่วนดังนี้
  1. selector (จากตัวอย่างคือ “body”) ซึ่งเป็นตัวบอกบราวเซอร์ว่าส่วนใดของเอกสารที่มีการนำกฏมาใช้
  2. property (จากตัวอย่างคือ 'color' และ 'background-color' ซึ่งเป็น property ทั้งสองตัว) ซึ่งเป็นตัวบอกว่าเราจะกำหนด layoutในรูปแบบไหน
  3. และ value ('purple' and '#d8da3d')ซึ่งเป็นตัวบอกค่า value ของ style property
ตัวอย่างข้างล่างนี้แสดงให้เห็นการรวมกันของกฏ เราได้กำหนด property ไว้สองตัวดังนั้นเราจึงได้แยกกฏออกมาเป็น 2 กฏดังนี้
body { color: purple }
body { background-color: #d8da3d }
แต่เนื่องจากกฎทั้งสองใช้กับ body ทั้งคู่ดังนั้นเราจะเขียน “body” แค่เพียงครั้งเดียวเท่านั้นและใส่ propert และ value ไว้ด้วยกันสำหรับเรื่องเกี่ยวกับ selector ให้อ่านเพิ่มได้ในบทที่2 ของ Lie &Bos.
นอกจากนี้พื้นหลังของ element body ยังเป็นพื้นหลังของเอกสารทั้งหมด หากเราไม่ได้กำหนดพื้นหลังที่เฉพาะให้กับ element อื่นๆเช่น (p, li, address…) ตามค่า default แล้วก็จะไม่มีค่าพื้นหลัง (หรืออาจจะเป็นแบบโปร่งแสง) ค่า property 'color' จะเป็นตัวกำหนด สีของข้อความสำหรับ element body แต่สำหรับ element อื่นๆที่อยู่ภายใน body จะมีสีตามค่า body เช่นกันยกเว้นว่าจะมีการกำหนดค่าไว้ต่างหาก (ซึ่งเราจะเพิ่มสีอื่นๆเข้าไปในภายหลัง)
ตอนนี้ให้เซฟไฟล์ดังกล่าวไว้ (โดยใช้คำสั่ง “Save” จากเมนู File) และกลับไปยังหน้าบราวเซอร์หากคุณกดปุ่ม “Reload” หน้าตา ของเวบจะเปลี่ยนไปจากหน้าตาที่ธรรมดาและน่าเบื่อเป็นหน้าที่มีสี (แต่ก็ยังดูค่อนข้างหน้าเบื่อ) นอกจากรายชื่อของลิงค์ด้านบน ตอนนี้ ข้อความควรจะเป็นสีม่วงซึ่งตัดกับสีพื้นหลังคือสีเหลืองอมเขียว
ภาพจากหน้าจอที่แสดงหน้าสีใน Konqueror
ภาพตัวอย่างเป็นการแสดงให้เห็นว่าบราวเซอร์แสดงผลอย่างไรเมื่อได้มีการเพิ่มสีเข้าไปบางส่วน
เตือน! ล่วงหน้า: เราสามารถกำหนดสีใน CSS ได้หลายวิธี ตัวอย่างข้างต้นได้แสดงไว้ทั้งสองวิธีคือใช้ทั้งชื่อ (“purple”) และค่าโคด hexadecimal (“#d8da3d”)สามารถใช้ชื่อสีได้ประมาณ 140 ชื่อและ ค่าโคด hexadecimal สามารถใช้สีได้มากกว่า 16 ล้านสี ในส่วนของการเพิ่มสไตล์ ได้มีการอธิบายเกี่ยวกับส่วนนี้ไว้


Read full post »

ขั้นที่ 1: การเขียน HTML

0 ความคิดเห็น
สำหรับบทความแนะนำนี้ ขอแนะนำให้ใช้เครื่องมือที่ใช้ง่ายที่สุดเช่น Notepad (สำหรับ Windows) TextEdit (สำหรับ Mac) หรือ KEdit (สำหรับ KDE) เมื่อคุณเข้าใจหลักการแล้วคุณอาจสามารถเปลี่ยนมาใช้เครื่องมือขั้นสูงกว่าได้หรือใช้โปรแกรมที่นิยมใช้กันทั่วไปเช่น Style Master Dreamweaver หรือ GoLive
อย่าใช้โปรแกรมเขียนเช่น Microsoft Word หรือ OpenOffice เนื่องจากโปรแกรมเหล่านี้จะสร้างไฟล์ที่ เวบบราวเซอร์ไม่สามารถอ่านได้ สำหรับ HTML และ CSS นั้นเราต้องการไฟล์ข้อความที่มีความเรียบง่าย
ขั้นแรกก็คือให้เปิดโปรแกรมสร้างข้อความ (Notepad, TextEdit, KEdit, หรืออะไรก็ตามที่คุณชอบใช้) เริ่มจากหน้าที่ว่างและพิมพ์ ข้อความต่อไปนี้ลงไป
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <title>My first styled page</title>
</head>

<body>

<!-- Site navigation menu -->
<ul class="navbar">
  <li><a href="index.html">Home page</a>
  <li><a href="musings.html">Musings</a>
  <li><a href="town.html">My town</a>
  <li><a href="links.html">Links</a>
</ul>

<!-- Main content -->
<h1>My first styled page</h1>

<p>Welcome to my styled page!

<p>It lacks images, but at least it has style.
And it has links, even if they don't go
anywhere&hellip;

<p>There should be more here, but I don't know
what yet.

<!-- Sign and date the page, it's only polite! -->
<address>Made 5 April 2004<br>
  by myself.</address>

</body>
</html>
ความจริงแล้วคุณไม่จำเป็นต้องพิมพ์เพียงแค่ก๊อปปี้ข้อความบนหน้านี้และนำไปวางไว้ในโปรแกรม
(ถ้าคุณใช้โปรแกรม TextEdit สำหรับ Mac อย่าลืมกำหนดใน TextEdit ให้เป็นแค่ข้อความธรรมดาเท่านั้นโดยไปตรงเมนู Format และเลือก "สร้างข้อความธรรมดา")
เตือน! ล่วงหน้า: บรรทัดแรกของไฟล์ HTML ข้างต้นเป็นการบอกบราวเซอร์ว่าเป็น HTML ประเภทใด (DOCTYPE หมายถึง DOCument TYPE) ในกรณีนี้คือ HTML version 4.01
คำที่อยู่ภายใน < และ > เรียกว่า tags ดังที่คุณได้เห็นว่า เอกสารจะอยู่ภายใน <html> และ </html> tags ระหว่าง <head>และ </head> จะมีข้อมูลหลายรูปแบบที่ไม่แสดงผลบนหน้าจอ ส่วนใหญ่แล้วจะประกอบไปด้วย title ของเอกสารแต่ภายหลังเราจะเพิ่ม CSS style sheet ลงไปในส่วนนี้เช่นกัน
ส่วน <body>นั้นจะเป็นส่วนที่วางข้อความจริงๆในเอกสาร ตามหลักการแล้วทุกอย่างที่อยู่ใน body tag จะถูกแสดงบนบราวเซอร์ยกเว้นข้อความที่อยู่ระหว่าง<!-- และ --> ซึ่งไว้ใช้ใส่คอมเมนต์สำหรับเราเอง ซึ่งบราวเซอร์จะไม่อ่านค่าดังกล่าว
จาก tag ในตัวอย่าง <ul>จะใช้สำหรับ "รายการที่ไม่ได้เรียงตามลำดับ" เป็นรายการที่ไม่ได้เรียงตามหมายเลข tag <li>จะเป็นตัว เริ่มต้นสำหรับสิ่งที่อยู่ในรายการ tag <p> ใช้สำหรับ "ย่อหน้า" และ tag<a>ใช้สำหรับ “Anchor” ซึ่งไว้ใช้ในการลิงค์
โปรแกรม KEdit editor ที่แสดงโคด HTML
โปรแกรม KEdit editor ที่แสดงโคด HTML
เตือน! ล่วงหน้า: ถ้าคุณอยากจะรู้ว่าชื่อที่อยู่ใน <…>มีความหมายว่าอะไร ที่ๆเหมาะสำหรับการเริ่มต้นอ่านคือ Getting started withHTML (เริ่มต้นด้วย HTML) แต่ไม่ได้กล่าวถึงโครงสร้างของหน้า HTML มากนัก
  • tag "ul" ใช้แสดงรายการที่เป็น link ต่อรายการหนึ่งซึ่งในที่นี้ก็คือ "เมนู navigation ของเวบไซด์" โดยใช้ลิงค์ไปยังหน้าอื่นๆภายในเวบไซด์ สมมติว่าทุกหน้าของเวบไซด์มีเมนูเหมือนกันหมด
  • element "h1" และ "p" ทำให้เกิดเนื้อหาที่มีลักษณะเด่นภายในหน้านี้ ในขณะที่ ลายเซ็นต์ในส่วนท้าย ("address") จะเหมือนกันในทุกๆหน้าของเวบไซด์
ข้อสังเกตก็คือไม่ได้มีการปิด tag ของ element “li” และ “p” ใน HTML (แต่ไม่ใช่ใน XHTML) สามารถละ tag ปิด</li> และ </p> ได้ซึ่งในที่นี้ได้ละไว้เพื่อให้ง่ายต่อการอ่าน แต่คุณอาจจะใส่ tag ปิดก็ได้ถ้าคุณต้องการ
สมมติว่าหน้านี่เป็นหนึ่งหน้าของเวบไซด์ที่มีหน้าอื่นๆที่คลายคลึงกัน ดังเช่นหน้าเวบปัจจุบันทั่วๆไปเวบนี้มีเมนูเพื่อลิงค์ไปยังหน้าอื่นๆในเวบ ที่เราสมมติขึ้นมานี้ซึ่งมีลายเซ็นต์และเนื้อหาที่มีลักษณะเฉพาะ
ทีนี้ลองเลือก “Save As…” จาก เมนู file และเลือกแฟ้มเก็บเอกสารที่คุณต้องการจะเซฟเก็บไว้ (หรือเซฟไว้บน เดสก์ทอปก็ได้) และให้เซฟไฟล์ดังกล่าวเป็น “mypage.html” แต่อย่าเพิ่งปิดโปรแกรมดังกล่าว เราจะต้องใช้งานมันอีก
(หากคุณใช้โปรแกรม TextEdit สำหรับ Mac OS X ก่อนเวอร์ชั่น 10.4 คุณจะพบว่ามี option ให้เลือกว่า อย่าแนบ .txt ต่อท้ายในการเซฟ ให้เลือก option ดังกล่าว เนื่องจากชื่อ “mypage.html” นั้นมีสกุลต่อท้ายอยู่แล้ว เวอร์ชั่นใหม่ของ TextEdit จะสังเกตเห็นว่ามี .html ต่อท้ายอยู่แล้วโดยอัตโนมัติ)
ต่อไป ให้เปิดไฟล์ใน บราวเซอร์ คุณสามารถเลือกทำได้ดังต่อไปนี้ ค้นหาไฟล์ที่เซฟไว้ด้วย ไฟล์เมเนเจอร์ (Windows Explorer, Finder หรือ Konqueror) และให้คลิกหรือดับเบิ้ลคลิกไฟล์ “mypage.html” ซึ่งน่าที่จะเปิดในบราวเซอร์ที่ตั้งไว้เป็น default (หากไม่เป็นเช่นนั้นให้เปิดบราวเซอร์และลากไฟล์ดังกล่าวมาใส่ไว้)
คุณจะเห็นดังนี้ ซึ่งหน้าตานั้นดูค่อนข้างที่จะธรรมดาน่าเบื่อ
Read full post »

วันอังคารที่ 25 กันยายน พ.ศ. 2561

HTML5 Web Storage คืออะไร ? + สอนวิธีใช้

0 ความคิดเห็น
แต่ก่อนเราอาจจะเคยใช้ cookie ในการจัดเก็บข้อมูลของ users แต่สำหรับ html5 แล้ว มีอีกวิธีที่น่าสนใจ นั่นก็คือการใช้ web storage

การเก็บข้อมูลไว้ที่ฝั่ง Client มันดียังไง ?

โดยทั่วไปแล้ว เว็บต่างๆ มักจะเก็บข้อมูลเอาไว้ที่ฝั่ง server แต่จริงๆ แล้ว การเก็บข้อมูลเอาไว้ที่ฝั่ง client ก็สามารถทำได้เช่นกัน และยังมีข้อดีตรงที่
  • เข้าถึงข้อมูลได้ด้วย JavaScriptในบางครั้งเราต้องการนำข้อมูลไปใช้ด้วย JavaScript ซึ่งเป็นภาษาฝั่ง client ที่สามารถเข้าถึงข้อมูลที่เก็บเอาไว้ที่ฝั่ง client ได้โดยสะดวก
  • ลดภาระของ Serverการเก็บข้อมูลไว้ที่ฝั่ง client เหมือนเป็นการกระจายภาระของ server ไปให้ทางฝั่ง client

เพราะ Cookie ไม่ดีพอ

หากเราต้องการจะเก็บข้อมูลเอาไว้ที่ฝั่ง client หลายๆ คนคงจะนึกถึง cookie แต่เนื่องจากมันถูกสร้างขึ้นมาเพื่อประโยชน์สำหรับภาษา script ทางฝั่ง server เป็นหลัก ทำให้มันมีข้อจำกัดดังนี้
  • ช้าในทุกๆ ครั้งที่เกิด HTTP request ข้อมูล cookie จะต้องถูกส่งไปด้วย ส่งผลให้การส่งผ่านข้อมูลนั้นช้าลง
  • เล็กและด้วยเหตุผลข้างต้น ขนาดของ cookie จึงถูกจำกัดไว้เพียงแค่ 4 KB เท่านั้น ซึ่งนั่นอาจไม่เพียงพอ
ด้วยข้อจำกัดดังกล่าว ใน html5 จึงได้มีการจัดเก็บข้อมูลในรูปแบบใหม่ที่เรียกว่า web storage

รู้จักกับ HTML5 Web Storage

web storage ใน html5 จะใช้สำหรับจัดเก็บข้อมูลเอาไว้ที่ฝั่ง client เหมือนๆ กับ cookie เลย เพียงแต่มันจะถูกออกแบบมาเพื่อให้ใช้กับภาษาฝั่ง client โดยเฉพาะ ทำให้มันมีข้อได้เปรียบเหนือ cookie ตรงที่
  • เร็วกว่าข้อมูลจาก web storage จะไม่ถูกส่งไปพร้อมกับ HTTP request แต่จะถูกนำมาใช้ก็ต่อเมื่อมีการเรียกใช้เท่านั้น
  • ใหญ่กว่าขนาดของ web storage นั้นใหญ่กว่า cookie หลายเท่า (5MB ต่อ 1 โดเมน)
การใช้ web storage ยังถูกแบ่งออกเป็น 2 แบบด้วยกัน ได้แก่ localStorage และ sessionStorage ทั้งนี้ก็เพื่อให้เหมาะสมในการใช้งานที่หลากหลาย

localStorage vs. sessionStorage

เราสามารถแบ่ง web storage ตามลักษณะการใช้งานได้เป็น 2 แบบ ด้วยกัน ดังนี้
  • localStorageข้อมูลที่เก็บด้วย localStorage จะเก็บไปตลอดกาล ไม่มีวันหมดอายุ
  • sessionStorageข้อมูลที่เก็บด้วย sessionStorage จะหายไปเมื่อมีการปิด window หรือ tab นั้นๆ
เราจะเห็นว่า localStorage และ sessionStorage นั้นเหมือนกันแทบทุกประการ สิ่งเดียวที่มันต่างกันก็คือ “ระยะเวลา” ที่จะเก็บข้อมูลเอาไว้นั่นเอง

วิธีใช้งาน Web Storage ขั้นพื้นฐาน

ทั้ง localStorage และ sessionStorage ต่างก็เป็น object สำหรับจัดเก็บข้อมูล และทั้งคู่ยังมี methods และ property ที่เหมือนกันทุกประการ ดังนี้
  • setItem(key, value)เก็บข้อมูล
  • getItem(key)ดึงข้อมูลที่เก็บไว้ออกมาใช้ ตาม key ที่ระบุ
  • removeItem(key)ลบข้อมูลที่เคยเก็บไว้ ตาม key ที่ระบุ
  • key(n)แสดงชื่อของ key ตาม index ที่ระบุ (เริ่มที่ 0)
  • clear()ลบข้อมูลที่จัดเก็บไว้ทั้งหมด (เฉพาะโดเมนนั้นๆ)
  • lengthแสดงจำนวนข้อมูลที่จัดเก็บไว้ทั้งหมด (เฉพาะโดเมนนั้นๆ)
ทีนี้เราลองมาดูตัวอย่างการใช้งาน localStorage แบบพื้นฐานกันก่อน
แต่หากเราต้องการจะล้างข้อมูลที่ได้เก็บไปทั้งหมดเมื่อมีการปิด window หรือ tab ก็ให้เราเปลี่ยนมาใช้ sessionStorage แทน โดยการเปลี่ยนชื่อ object จาก localStorage มาเป็น sessionStorage

Web Storage เก็บได้แต่ String !

ข้อมูลที่ต้องการจะจัดเก็บด้วย web storage จะต้องอยู่ในรูปแบบ string เท่านั้น หากเราต้องการจะจัดเก็บ object เราจะต้องแปลงมันให้เป็น string ก่อนเสมอ

เราสามารถนำ Web Storage ไปทำอะไรได้บ้าง ?

เราสามารถนำความสามารถของ web storage ไปประยุกต์ใช้กับ web application ได้หลายรูปแบบด้วยกัน ดังนี้
  • เก็บค่า Preferencesใช้เก็บค่าต่างๆ ที่ users สามารปรับแต่งได้เอง เช่น ขนาดตัวอักษร หรือมุมมอง เป็นต้น
  • เก็บสถานะการใช้งานล่าสุดใช้เพื่อเก็บรักษาสถานะของ web application หรือค่า input ต่างๆ ในฟอร์ม ให้คงอยู่เหมือนเดิมแม้ว่าจะปิด web application ไปแล้ว
  • Cache ข้อมูลใช้เก็บข้อมูลบางส่วนที่ไม่ต้องการการอัพเดทแบบ real-time เพื่อจะได้ลดภาระของ server ให้น้อยลง
มาถึงตอนนี้ เรารู้แล้วว่า web storage สามารถทำอะไรได้บ้าง ทีนี้เราลองมาลงมือใช้ web storage จริงๆ กันเลยดีกว่า

Workshop – เก็บ Preferences ด้วย localStorage

สมมติว่าเว็บเรามีฟังก์ชันให้ users สามารถปรับขนาดตัวอักษรได้ด้วยตัวเอง เราไม่อยากให้ users ต้องมากำหนดใหม่ทุกครั้งที่เปลี่ยนหน้าเว็บหรือตอนเข้าเว็บใหม่ เราจึงตัดสินใจจะใช้ web storage เข้ามาช่วยเก็บ “ขนาดของตัวอักษร” ที่ users ได้เลือกไว้
ก่อนอื่นเราต้องเขียน html สำหรับส่วนของฟังก์ชันเปลี่ยนขนาดตัวอักษรและส่วนสำหรับแสดงเนื้อหาขึ้นมาก่อน โค้ด html ของเราจะได้ประมาณนี้
ต่อมาให้เราเขียนโค้ด javascript เพื่อใช้เปลี่ยนขนาดตัวอักษรให้เป็นไปตามที่ users เลือก และเก็บลง web storage ด้วย เพื่อที่เวลาเปลี่ยนหน้าใหม่ users จะได้ไม่ต้องมาเลือกอีก
จะเห็นว่าในกรณีนี้เราเลือกใช้ localStorage เนื่องจากการใช้ sessionStorage จะทำให้ users ต้องมาเลือกขนาดตัวอักษรใหม่ทุกครั้งหากเค้าปิดเว็บไปแล้ว

Workshop – เก็บสถานะล่าสุดด้วย localStorage

ในบางครั้ง เราอาจต้องการอำนวยความสะดวกให้กับ users โดยการนำข้อมูลที่เค้ากรอกในฟอร์มครั้งล่าสุดมาแสดงรอไว้เลย เพื่อที่เค้าจะได้ไม่ต้องมากรอกข้อมูลใหม่หมดทุกครั้ง
สมมติเว็บเรามีฟอร์มสำหรับลงชื่อเข้าสู่ระบบ เราอาจเพิ่ม feature สำหรับจดจำข้อมูล username รวมไปถึง password เพื่อที่ users จะได้รับความสะดวกในการลงชื่อเข้าสู่ระบบมากขึ้น โค้ด html ของเราจะเป็นแบบนี้
เมื่อมีการ submit ฟอร์ม เราจะทำการบันทึกข้อมูลทั้งหมดในฟอร์มลงไปใน web storage และมีการเช็คทุกๆ ครั้งตอนโหลดหน้าเว็บ ว่าถ้าข้อมูลของฟอร์มนั้นมีอยู่ใน web storage แล้ว ให้นำข้อมูลเหล่านั้นมาแสดงในฟอร์มได้เลย โค้ด javascript ของเราจะได้ประมาณนี้
อย่างไรก็ตาม การเก็บข้อมูลสำคัญๆ ลงใน web storage อาจไม่ปลอดภัยเท่าไรนัก หากเราจะใช้ feature นี้กับข้อมูลที่เป็นความลับ เราจะต้องถาม users ก่อนเสมอว่าเครื่องคอมพิวเตอร์เครื่องนี้เป็นเครื่องส่วนตัวหรือไม่

Workshop – Cache ข้อมูล ด้วย localStorage

สำหรับข้อมูลที่ไม่ได้มีการอัพเดทบ่อยมากนัก เราอาจเก็บเอาไว้ที่ฝั่ง client เลยก็ได้ และพอถึงเวลาที่มีการอัพเดท เราก็ค่อยไปล้างข้อมูลที่เครื่องของ users แล้วจึงเก็บข้อมูลที่เพิ่งอัพเดทใหม่เข้าไปแทน
สมมติเว็บเรามีส่วนที่เอาไว้แสดง content จาก feed เราไม่อยากให้ส่วนนี้ต้องเชื่อมต่อไปยังฐานข้อมูลของ server ทุกครั้ง เพราะข้อมูลนั้นนานๆ จะอัพเดทซะที เราจึงใช้วิธีเก็บ content จาก feed ลงใน web storage แล้วอ่าน content จากในนั้นแทน
โค้ด html ของส่วนแสดง content จาก feed จะเป็นแบบนี้
จากนั้นเราจะต้องเขียนโค้ด javascript เพื่อตรวจดูว่าใน web storage มี content จาก feed เก็บเอาไว้แล้วหรือยัง ถ้ามีแล้ว ให้นำมาแสดงได้เลย แต่ถ้ายังไม่มี ให้ไปเอา content มาจาก feed เสียก่อน แล้วจึงเก็บ content เหล่านั้นลง web storage คราวหน้าเราจะได้ไม่ต้องไปอ่านจาก feed อีก

ข้อเสียของ Web Storage

อย่างไรก็ตาม web storage ก็ยังมีข้อเสียอยู่บ้าง ดังนี้
  • ไม่มีการกำหนดอายุในบางกรณี เราอาจต้องการกำหนดอายุของข้อมูลที่จะจัดเก็บเหมือนกับที่ cookie ทำได้
  • ไม่ปลอดภัยข้อมูลที่ถูกจัดเก็บด้วย web storage จะอยู่ในรูป string ที่อ่านรู้เรื่อง และยังสามารถดูได้ผ่านทาง web browsers อีกด้วย

ความปลอดภัยของข้อมูล

การใช้ web storage ในการเก็บข้อมูลที่เป็นความลับ มีข้อควรระวังดังนี้
  • ใครๆ ก็สามารดูข้อมูลใน web storage ที่เก็บอยู่บนเครื่องนั้นๆ ได้อย่างง่ายดาย เพียงแค่พิมพ์ localStorage หรือ sessionStorage ไปที่ console ของ firebug
  • ถึงแม้ว่า web storage จะถูกจำกัดการใช้งานไว้เพียงแค่ domain ที่ได้สร้างข้อมูลขึ้นมาเท่านั้น แต่หาก domain นั้นถูกโจมตีด้วย XSS แล้วล่ะก็ ข้อมูลเหล่านั้นก็จะสามารถถูกเข้าถึงโดยผู้ที่โจมตีได้อยู่ดี
  • ถึงแม้ว่าเราจะเข้ารหัสข้อมูลก่อนที่จะเก็บด้วย web storage แต่มันก็จะทำให้มีขั้นตอนในการถอดรหัสเพิ่มเข้ามา และการถอดรหัสที่ฝั่ง client ก็คงไม่ปลอดภัยเท่าฝั่ง server
ด้วยเหตุผลดังกล่าว เราจึงไม่ควรใช้ web storage ในการเก็บข้อมูลที่เป็นความลับเป็นอันขาด

localStorage with firebug
พิมพ์ localStorage ที่ console ของ firebug เพื่อดูว่า Mashable ใช้ localStorage เก็บข้อมูลอะไรเอาไว้บ้าง

Browsers Compatibility

เราสามารถใช้ web storage ได้แล้ววันนี้ เนื่องจาก web browsers หลักๆ รองรับแล้วทั้งหมด แม้แต่ Internet Explorer ก็ยังรองรับแล้วตั้งแต่เวอร์ชัน 8

Cookie vs. Web Storage

หากเราพิจารณาดูดีๆ จะเห็นว่า web storage นั้น ไม่ได้มาแทน cookie เลยซะทีเดียว เพราะในบางกรณี การใช้ cookie ก็ดูจะเหมาะสมกว่า ในการตัดสินใจว่าเราควรจะเลือกใช้ web storage หรือ cookie ดีนั้น ให้เราถามตัวเองว่าเราจะเก็บข้อมูลเพื่อให้ใครเอาไปใช้ ? หากเป็นฝั่ง server ให้เราใช้ cookie แต่หากเป็นฝั่ง client ก็ให้เราใช้ web storage แทน
Demo1 – CounterDemo2 – PreferenceDemo3 – FeedDemo4 – FormDownload Source
Read full post »

พื้นที่โฆษณา

Free Hosting

พื้นที่โฆษณา

Free Hosting
 

Copyright © สอนเขียนโปรแกรม html php css Java SQL jQuery XML Ajax Design by ScriptMasterWebDesign | Theme by ScriptMasterWebDesign | Powered by HosTing