Friday, October 17, 2014

FireFox page-break-before After DIV Problem

FireFox เมื่อใช้ page-break-before ใต้ <div> ในหน้าพิมพ์หน้าที่ 2 มักเกิดปัญหาตัวหนังสือขึ้นมากองอยู่บนบรรทัดแรกหมด วิธีแก้ไขปัญหานี้จากยูสเซอร์ RivendellH

อ้างอิงจาก
http://forums.mozillazine.org/viewtopic.php?f=25&t=278578

Example: 
<div> 
... 
</div> 
<br style="page-break-before: always;" clear="all" /> 
does not create a page break in a print preview or the actual printing. 

This works: 
<p> 
... 
</p> 
<br style="page-break-before: always;" clear="all" /> 

This works: 
<div> 
... 
</div> 
<p><br style="page-break-before: always;" clear="all" /></p> 

And this works: 
<div> 
... 
</div> 
<br style="page-break-after: always;" clear="all" />

Tuesday, August 5, 2014

ปัญหา document.getElementById('myInput').focus(); ไม่ทำงานบน IE

ปัญหานี้มักเจอเวลาที่สั่งให้ focus ทันทีที่โหลดหน้าขึ้นมา ซึ่งถ้าเป็น browser อื่นๆที่ไม่ใช่ IE จะไม่มีปัญหาอะไร แต่ถ้าต้องการให้ทำงานได้บน IE ด้วยจำเป็นต้องใส่ delay เข้าไปด้วยเพราะบางทีถ้าหน้าไหนที่โหลดหน้าช้า IE จะไม่ทำคำสั่งย้อนหลังให้
ตัวอย่างการใช้งาน

<html>
 
<head>
<title>focus is working in IE</title>
</head>
<body>
 
<form >
    <input id="myInput" type="text" value='testing'><br>
</form>
</body>
 
 <script type="text/javascript">
 setTimeout(function() { document.getElementById('myInput').focus(); }, 1000);
</script>
 
</html>
อ้างอิงจาก http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/

Friday, February 21, 2014

การทำเว็บไซต์ไม่ให้ Chrome ถามเวลาที่ดาวน์โหลดไฟล์ zip ที่มีไฟล์ .exe อยู่ข้างใน

สามารถทำได้โดยการเพิ่ม Tag Content Type Zip เข้าไปที่ส่วน Header ของไฟล์เช่น
<head>
<meta http-equiv="Content-Type" content="HTML,CSS,XML,JavaScript">
<meta http-equiv="Content-Type" content="application/zip .zip">
<meta charset="UTF-8">
</head>

อ้างอิงจาก
http://www.sitepoint.com/forums/showthread.php?940794-File-(-zip)-is-not-commonly-downloaded-and-could-be-dangerousded-and-could-be-dangerous

Saturday, January 25, 2014

การทำปุ่ม Home, End บน Mac. OS ให้เหมือน Windows OS

ปัญหาหนึ่งที่พบเจอบ่อยๆ คือความคุ้นเคยในการใช้ text editor บน windows แล้วพอสลับไปใช้เครื่อง mac. คือการหลงไปกดปุ่ม Home หรือ End แทนที่จะไปที่หัวหรือท้ายบรรทัด แต่กลับกระโดดไปที่ส่วนบนสุดหรือล่างสุดของไฟล์ทำให้เสียเวลามาก วิธีแก้คือเข้าไปเพิ่มไฟล์ DefaultKeyBinding.dict เข้าไปในระบบ

To get your Home and End keys working properly on Mac OS X (in my case, Mountain Lion, although this should work in prior versions back to at least Tiger), simply open the Terminal and do this:

$ cd ~/Library
$ mkdir KeyBindings
$ cd KeyBindings
$ nano DefaultKeyBinding.dict

Put these lines in that file, including the curly braces:

{
/* Remap Home / End keys to be correct */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
"^\UF729" = "moveToBeginningOfDocument:"; /* Ctrl + Home */
"^\UF72B" = "moveToEndOfDocument:"; /* Ctrl + End */
"$^\UF729" = "moveToBeginningOfDocumentAndModifySelection:"; /* Shift + Ctrl + Home */
"$^\UF72B" = "moveToEndOfDocumentAndModifySelection:"; /* Shift + Ctrl + End */
}

Press Ctrl+O and then Enter to save the file, and Ctrl+X to exit. Restart your computer to have it take full effect.

แต่ในบางโปรแกรมที่มีความฉลาดในการกำหนดคีย์เองอยู่แล้วก็สามารถเข้าไปกำหนดได้เลย เช่น Eclipse โดยเข้าไปที่ Preferences -> General.Keys -> Text Editing

source:
http://mwholt.blogspot.com/2012/09/fix-home-and-end-keys-on-mac-os-x.html
http://www.kelek.com/blog/2006/09/16/fixing_home_end.html

Monday, September 9, 2013

Android ปัญหาการใช้ HttpGet และ HttpPost กับ Android version 3 ขึ้นไป

เนื่องจาก Google ได้มีการบังคับ ThreadPolicy ที่เกี่ยวกับการติดต่ออินเตอร์เน็ตไม่ให้สามารถทำงานตรงๆบน UI Thread ได้ต้องใช้ AsyncTask และ doInBackground เข้ามาใช้งานด้วย ตัวอย่าง

    private class requestHttpTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
          String response = "";
          for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
              HttpResponse execute = client.execute(httpGet);
              if(execute.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
      {
     
      //http ok///

      }

            } catch (Exception e) {
              //e.printStackTrace();
            }
          }
          return response;
        }

        @Override
        protected void onPostExecute(String result) {
          //textView.setText(result);
        }

      }

วิธีเรียกใช้งาน


requestHttpTask task = new requestHttpTask();

task.execute(new String[] { "http://www.tonnark.com" });


อ้างอิงจาก
http://www.elvenware.com/charlie/development/android/SimpleHttpGetThread.html

Tuesday, September 3, 2013

Android คำสั่ง finish(); และ System.exit(0);

คำสั่ง finish() จะเป็นแค่การบอกว่าจบการทำงานแล้ว แต่ถ้าในโปรแกรมของเรายังรัน service หรือ Listener ใดๆอยู่ โปรแกรมก็จะยังทำงานต่อไปจนกว่าจะจบ service หรือ Listener นั้น
ดังนั้นบางคนที่ใช้คำสั่ง finish() แล้วยังสงสัยว่าทำไมโปรแกรมยังรันเบื้องหลังอยู่ไม่ยอมปิดจริงๆ ก็เพราะสาเหตนี้

ส่วนคำสั่ง System.exit(0) จะเป็นการบังคับให้โปรแกรมหยุดทำงานทันทีไม่ว่าจะมี service หรือ Listener ใดๆอยู่ก็ตาม เปรียบเหมือนการ force close ซึ่งในการใช้งานต้องระวังให้มากและต้องแน่ใจว่าไม่มีโพรเซสใดค้างอยู่แล้วไม่งั้นอาจทำให้โปรแกรม error ได้ เช่นมีคำสั่ง save หรือเขียนข้อมูลอยู่ซึ่งยังทำไม่เสร็จแล้วใช้คำสั่ง System.exit(0) จะทำให้การเซฟนั้นไม่สมบูรณ์และไฟล์ข้อมูลอาจเสียหายได้

Tuesday, August 13, 2013

Android Bluetooth Communication

การเขียนแอพพลิเคชั่นเพื่อใช้งาน Bluetooth บนระบบปฏิบัติการที่เป็น Android นั้นสามารถดูรายละเอียดได้ที่ http://developer.android.com ตัวอย่างแอพพลิเคชั่นที่ Google มีให้ดาวน์โหลด และหลายๆเว็บไซต์นิยมนำมาใช้อ้างอิงหรือยกตัวอย่างคือ Bluetooth Chat โดยดูวิธีการดาวน์โหลด Source Code ได้ที่ http://developer.android.com/tools/samples/index.html
การทำงานของ Bluetooth Chat จะเป็นการสื่อสารระหว่างอุปกรณ์ที่เป็น Android เหมือนกันโดยผ่านทาง Bluetooth และสามารถเข้าไปทดลองเล่นตัวแอพพลิเคชั่น Bluetooth Chat นี้ได้ที่ Google Play
Android <—— Bluetooth —–> Android
นอกจากนั้นเรายังสามารถนำ Source Code มาดัดแปลงใช้กับอุปกรณ์อื่นๆที่ไม่ใช่ Android ก็ได้ ยกตัวอย่างเช่น นำมาใช้กับ Microcontroller เพื่อความคุมการทำงานของอุปกรณ์ต่างๆได้
Android <—– Bluetooth —–> Bluetooth Module* <—–> Microcontroller
อันนี้เป็นตัวอย่างที่ผมทดสอบแบบง่ายๆโดยการทำปุ่ม ON, OFF เพื่อความคุมการเปิดปิดของหลอด LED
* ตัวอย่าง Bluetooth Module ที่ราคาไม่แพงและใช้งานง่ายเช่น JY_MCU Bluetooth Module จะมีหน้าตาเป็นแบบนี้
ค่า Default ของตัวโมดูลนี้คือ Default serial port setting : 9600, N, 8, 1; Pairing code : 1234
ส่วนค่าอีกตัวหนึ่งของโมดูลที่จะใช้ที่ต้องรู้คือ Unique identifier ซึ่งจะอยู่ในรูปของตัวเลขฐาน 16 จำนวน 6 ชุด เช่น CC:8C:E3:DC:63:20 ถ้ามีพีซีหรือโน๊ตบุคที่เป็น Windows 7 และที่ตัวเครื่องมี Bluetooth อยู่แล้วสามารถดูได้ดังนี้
1. ไปที่ Devices and Printer เลือก Add a device
2. เมื่อ Windows เริ่มค้นหาอุปกรณ์ก็ต่อไฟเลี้ยงเข้าที่ตัวโมดูล แล้วรอซักครู่ Windows จะแจ้งว่าเจออุปกรณ์ BT UART Bluetooth Other
3. คลิกขวาที่ตัว BT UART ที่ Windows เจอแล้วเลือก Properties
4. กดที่ Tab Bluetooth ดูที่ด้านล่างจะมีบรรทัดที่เขียนว่า Unique identifier
เมื่อเราได้ Unique identifier ของ Bluetooth Module ที่จะใช้มาแล้วต่อไปจะเป็นขั้นตอนการดัดแปลง Source Code ของแอพพลิเคชั่น Bluetooth Chat เพื่อให้อุปกรณ์ Android สามารถสื่อสารกับ Bluetooth Module นี้ได้มีขั้นตอนดังนี้
1. ไปที่ class BluetoothChatService หาบรรทัดที่พิมพ์ว่า
// Unique UUID for this application
private static final UUID MY_UUID = UUID.fromString(“fa87c0d0-afac-11de-8a39-0800200c9a66″);
แก้บรรทัดนี้เป็น
// Unique UUID for this application
//private static final UUID MY_UUID = UUID.fromString(“fa87c0d0-afac-11de-8a39-0800200c9a66″);
private static final UUID MY_UUID= UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);
2. ถ้าต้องการให้ทุกครั้งที่รันแอพพลิเคชั่นให้เชื่อมต่อกับ Bluetooth Module อัตโนมัติก็สามารถทำได้โดยเข้าไปที่ class BluetoothChat extends Activity แล้วไปที่ event onStart()
หาบรรทัดที่พิมพ์ว่า setupChat();
แล้วเพิ่มคำสั่งเข้าไปเป็น
setupChat();
String address = “CC:8C:E3:DC:63:20″;
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mChatService.connect(device);
เมื่อเราทำการ Paired ให้มือถือรู้จักกับ Bluetooth Module แล้วเพียงเท่านี้แอพพลิเคชั่น Bluetooth Chat ที่ทำการดัดแปลงแล้วนี้ก็จะสามารถเชื่อมต่ออัตโนมัติและสื่อสารกับ Bluetooth Module ที่มี Unique identifier = CC:8C:E3:DC:63:20 ได้