Category Archive HTML

พรีวิวรูปที่อัพโหลด

ถ้าเวลาเรา upload รูปขึ้น server คงจะดีถ้าเราเห็นว่าเราเลือกภาพถูกรึเปล่า หรือขนาดมันใหญ่เกินไปมั๋ย เราทำได้โดยการแสดงภาพ Preview ขนาด ความกว้าง ความสูง และทำการตรวจสอบก่อนจะได้ไม่เสียเวลา

ตัวอย่าง jQuery Upload Image Preview Demo

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Upload Image Preview</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
	<label>เฉพาะรูปภาพ
		<input type="file" name="pic" accept="image/*">
	</label>
</form>
<img id="picPreview">
<dl>
	<dt>image dimensions width</dt>
	<dd id="picDimensionsWidth"></dd>
	<dt>image dimensions height</dt>
	<dd id="picDimensionsHeight"></dd>
	<dt>lastModified</dt>
	<dd id="picLastModified"></dd>
	<dt>lastModifiedDate</dt>
	<dd id="picLastModifiedDate"></dd>
	<dt>name</dt>
	<dd id="picName"></dd>
	<dt>original file upload path</dt>
	<dd id="picPath"></dd>
	<dt>size</dt>
	<dd id="picSize"></dd>
	<dt>type</dt>
	<dd id="picType"></dd>
</dl>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
$('[name="pic"]').change(function(){

	/* original file upload path */
	$('#picPath').text($(this).val());

	var file = this.files[0];
	/* get picture details */
	$('#picLastModified').text(file.lastModified);
	$('#picLastModifiedDate').text(file.lastModifiedDate);
	$('#picName').text(file.name);
	$('#picSize').text(file.size);
	$('#picType').text(file.type);

	/* set image preview */
	if( ! file.type.match(/image.*/))
	{
		return true;
	}
	var reader = new FileReader();
	reader.onload = function (event)
	{
		$('#picPreview').attr('src', event.target.result);

		/* get image dimensions */
		var image = new Image();
		image.src = reader.result;
		image.onload = function()
		{
			$('#picDimensionsWidth').text(image.width);
			$('#picDimensionsHeight').text(image.height);
		};

	}
	reader.readAsDataURL(file);

});
</script>
</body>
</html>

เลือกชนิดไฟล์อัพโหลด

คุณสมบัติที่น่าสนใจอย่างหนึ่งของ HTML 5 คือเราสามารถกำหนดให้อินพุทยอมรับชนิดไฟล์ตามที่เราต้องการได้ ช่องใส่ภาพประจำตัว ก็น่าจะใส่ได้แค่นามสกุล jpeg อย่างน้อยก็ png หรือ gif แต่ก็มียูเซอร์ copy รูปใส่ word upload เข้ามา สมกับ used เซ่อๆ จริงๆ
เราสามารถกำหนดได้โดยใช้ accept Attribute ตามตัวอย่าง

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Form Input Accept</title>
<style>
label
{
	display: block;
}
</style>
</head>
<body>
<form action="" enctype="multipart/form-data">
	<label>เฉพาะไฟล์เสียง
		<input type="file" name="files[]" accept="audio/*">
	</label>
	<label>เฉพาะรูปภาพ
		<input type="file" name="files[]" accept="image/*">
	</label>
	<label>เฉพาะวีดีโอ
		<input type="file" name="files[]" accept="video/*">
	</label>
	<label>pdf
		<input type="file" name="files[]" accept="application/pdf">
	</label>
	<label>.gif หรือ .png
		<input type="file" name="files[]" accept="image/gif, image/png">
	</label>
	<label>.jpg
		<input type="file" name="files[]" accept="image/pjpeg, image/jpeg">
	</label>
	<input type="submit">
</form>
</body>
</html>

จะสังเกตุว่าเวลาเรา browse file มันจะแสดงเฉพาะนามสกุลที่เรากำหนด แต่ยังเปลี่ยนตัวเลือกไปเป็น All Files (*.*) ได้อยู่ดี มันแค่ช่วยให้เลือกไฟล์ได้ง่ายขึ้น ยังรับประกันไม่ได้อยู่ดีว่าจะได้นามสกุลไฟล์ที่ถูกต้อง หรือไฟล์ที่ส่งมานามสกุลถูกตาม Extension แต่ก็ไม่ได้รับรองว่าไฟล์มันเป็นจริงตามนามสกุลที่ส่งมา จุดที่อันตรายก็เลยต้องมาตรวจชนิดไฟล์ในผั่ง server อยู่เหมือนเดิม

ค่าที่ใส่ใน accept คือ MIME Types ดูเพิ่มเติมได้ที่ The Complete List of MIME Types

ใส่ Youtube ใช้ได้บนมือถือและทุกจอ

สมัยนี้เวลาทำเว็บต้องคิดถึงพวกอุปกรณ์ขนาดหน้าจอต่างๆ เมื่อใส่ video แล้วก็อยากจะให้มันกว้างพอดีกับหน้าจอ ไม่ว่าจะเป็น iPhone, ipad, Note อุปกรณ์พกพามันมีหลายขนาดเหลือเกิน โดยเฉพาะของช่างตัดกระจกอย่าง samsung ที่ทำมือถือออกมาแทบจะทุกขนาด แถมแต่ละเครื่องยังมีแนวตั้ง แนวนอนให้เลือกอีก อีกจะเขียน code ให้ได้ทุกชิ้นยังไงก็ไม่ครบทั้งหมด

ไปเจอบนความดีๆ Responsive Youtube Embed ใช้แค่ css นิดหน่อยก็ตอบโจทย์ได้แล้ว

เปิดหน้าเว็บยูทูปที่เราต้องการใส่ในเว็บ ของผมคือเพลง เพลง ความรักกับรองเท้า
ศิลปิน SIGNATURE สังเกตุด้านบนเว็บจะมี url อยู่ด้านบน ของเพลงนี้คือ http://www.youtube.com/watch?v=FPnewcqkmlc&list=RDFPnewcqkmlc คัดลอกอักษรระหว่าง v= และ & ไปใส่โค้ทตามตัวอย่างด้านล่าง

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Responsive Youtube Embed</title>
<style>
.video-container {
	height: 0;
	overflow: hidden;
	padding-bottom: 56.25%;
	padding-top: 30px;
	position: relative;
}
.video-container embed, .video-container iframe, .video-container object {
	height: 100%;
	left: 0;
	position: absolute;
	top: 0;
	width: 100%;
}
</style>
</head>

<body>
<div class="video-container">
  <iframe src="http://www.youtube.com/embed/FPnewcqkmlc" frameborder="0" width="560" height="315"></iframe>
</div>
</body>
</html>

เสร็จแล้ว มันง่ายมาก ฟังเพลงโปรดของผมไปเพลินๆ นะครับ หวังว่าวันหนึ่งจะได้มีคนมานั่งฟังเพลงนี้ด้วยกัน

ป้องกัน Copy Paste

ถ้าต้องการไม่ให้ user คัดลอกข้อมูลมาวางในช่องยืนยันอีเมล์ เราสามารถใช้คุณลักษณะ (attribute) onpaste ของเท็ก อินพุตช่วยป้องกันได้ ตามตัวอย่าง

<input type="text" name="confrimEmail" autocomplete="off" onDrag="return false;" onDrop="return false;" onPaste="return false;" />

แต่เนื่องจากโอเปร่าไม่สนับสนุนความสามารถนี้ (ตัวเดียว ?) ทำให้ต้องใช้จาว่าสคริปต์เต็มรูปแบบในการตรวจสอบ

<input type="text" name="confrimEmail" autocomplete="off" onDrag="return false;" onDrop="return false;" onPaste="return false;" onMouseDown="noRightClick(event);" onKeyDown="return noPaste(event);" />
<script>
function noPaste(event)
{
	var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
	if (event.ctrlKey && pressedKey == 'v')
	{
		alert('Sorry, this functionality is disabled.');
		return false;
	}
}

function noRightClick(event)
{
	if (event.button==2)
	{
		alert("Right Click Not Allowed!");
	}
}
</script>

มี 2 ฟังก์ชั่นคือ

  1. noRightClick ป้องกันการคลิกขวาทุกชนิด
  2. noPaste ป้องกันการวางโดนใช้ชอร์ตคัต Shortcut,ชอร์ตคัต คนที่เคยเขียนเกมส์มาจะห็นว่าความจริง เราใช้แค่ event.ctrlKey ก็ตรวจจับการใช้ ปุ่ม control ได้แล้ว ภาษาหรือเครื่องแมค บางครั้งต้องใช้ปุ่มนี้ในการพิมพ์อักษรบางตัว ทำให้ต้องใช้ event.keyCode มาจับว่าเป็นการวางข้อความ Ctrl + V รึเปล่า

ว่างๆ ลองดัดแปลงเป็นตรวจจับการจับภาพหน้าจอหรือพิมพ์หน้าเว็บดูนะครับ
ดูเพิ่มเติม

ให้ฟุตเตอร์อยู่ด้านล่างพอดีหน้าจอ (ตาราง)

จากที่เคยเขียน ให้ฟุตเตอร์อยู่ด้านล่างพอดีหน้าจอ
ถ้าหน้าเว็บสั้นกว่าจอโดยใช้ tableless เรามาย้อนยุคโดยใช้ตารางดูบ้าง
note

  • ใช้ได้กับทุกบราวเซอร์รวมทั้งไอโฟนด้วย แต่ ie ต้องใช้ DOCTYPE เท่านั้น ถ้าเป็นตัวอื่นจะกำหนดความสูงของแถวไม่ได้
  • ถ้าเนื้อหาน้อยมากๆ ตารางจะพยามชดเชยความสูงของตัวมันเองที่มากกว่าปกติ โดยจะเพิ่มความสูงของ tr ทดแทน ถ้าไม่อยากให้หน้าเว็บดูผิดปกติให้แก้
    ที่มีปัญหาเป็น

  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>100% table height</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="css3.css">
    <style>
    html
    {
    	height:100%;
    }
    body
    {
    	height:100%;
    	margin: 0px;
    	padding: 0px;
    	font-family: Helvetica, Arial, "Trebuchet MS", sans-serif;
    }
    table
    {
    	border: 0 none;
    	height:100%;
    }
    td
    {
    	margin: 0px;
    	padding: 0px;
    	vertical-align:top;
    	height:0px;
    }
    #header
    {
    	background:#c00000;
    	height:20px;
    }
    #footer
    {
    	background:#c00000;
    	height:20px;
    }
    </style>
    </head>
    <body>
    <table border="1" class="tablewrapper">
      <tr>
        <td id="header">header</td>
      </tr>
      <tr>
        <td>contents</td>
      </tr>
      <tr>
        <td id="footer">footer</td>
      </tr>
    </table>
    </body>
    </html>
    

    นี่ถ้าเขียนโดยใช้ div จะง่ายและสั้นกว่ามาก
    ดูเพิ่มเติม

ตารางใน html5

ใน html5 ได้ปรับปรุงโครงสร้างตารางให้สามารถอธิบายข้อมูลภายในได้มากขึ้น โดยมีโครงสร้าง

<table border="1">
  <caption>
  ตารางเงินเดือน
  </caption>
  <thead>
    <tr>
      <th scope="cal">เดือนที่</th>
      <th scope="cal">เดือน</th>
      <th scope="cal">รายได้</th>
      <th scope="cal">รวม</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>ม.ค.</td>
      <td>10,000</td>
      <td>10,000</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>ก.พ.</td>
      <td>1,000</td>
      <td>11,000</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>มิ.ค.</td>
      <td>1,040</td>
      <td>12,040</td>
    </tr>
  ...........
    </tbody>
  <tfoot>
    <tr>
      <td colspan="3">รวม</td>
      <td scope="cal">100,000</td>
    </tr>
  </tfoot>
</table>

จะเห็นส่วนที่เพิ่มมาคือ

  1. caption อธิบายรายละเอียดตาราง
  2. th จะใช้แทน td ในกรณีที่เป็นหัวตารางทั้งคอลัมน์และแถว (ทำไมไม่ใช้สดมและแถว ?) จะมี scope Attribute โดยมีค่า
    • col = คอลัมน์
    • colgroup = กลุ่มคอลัมน์
    • row = แถว
    • rowgroup = กลุ่มของแถว

    ถ้าเป็นคอลัมน์ที่เป็นผลรวมของทั้งตารางละ ?

  3. thead table tfoot คือ หัวตาราง เนื้อหา และท้ายตารางตามลำดับ

จะเห็นได้ว่าเป็นโครงสร้างที่สามารถอธิบายข้อมูลภายในได้มากกว่าเดิม น่าเสียดายที่ Adobe Dreamweaver ไม่ช่วยปรับพวก scope เวลารวมตารางต้องมาปรับเอง

สูตรคณิตศาสตร์‏

ถ้าทำเว็บอีเลิร์นนิ่งเกี่ยวกับการเรียนการสอนคณิตศาสตร์‏จะเขียนสูตรที่ซับซ้อนได้ยาก แนะนำให้ลง ASCIIMathML กับ TinyMCE จะใส่เนื้อหาได้ง่ายขึ้นเยอะ
ทดลองเขียนสูตรได้ที่
TinyMCE Math/Graph Plugins Example
http://www1.chapman.edu/~jipsen/mathml/asciimathdemo.html
http://www.imathas.com/editordemo/demo.html
มีตัวอย่างสูตรที่
http://www1.chapman.edu/~jipsen/mathml/asciimath.html

ขยาย iframe อัตโนมัติตามเนื้อหา

ถ้าต้องการให้ iframe ขยายอัตโนมัติตามเนื้อหาที่อยู่ข้างในเราสามารถใช้โค้ทด้านล่างปรับความสูงได้ครับ

<script>
function iframeHeight(obj){
    the_height= obj.contentWindow.document.body.offsetHeight;
    obj.height=the_height+10;
}
<iframe id="some" onload="iframeHeight(this)"></iframe>