หน้าหลัก โค้ด-บทความ php โค้ด-บทความ javascript โค้ด-บทความ css สารบัญ สารบัญ

ทักทายครับ

สวัสดีครับ หลังจากที่ blog นี้หยุดการอัพเดททบความเกี่ยวกับโค้ดในการใช้ทำเวปไปนานไม่ว่าจะเป็น code php, html, javascript, css, ajax เนื่องจากผมไม่ค่อยมีเวลาจนลืม blog นี้ไปเลย นึกขึ้นได้ตอนปีใหม่ ไปค้นหาใน google แล้วตกใจ เนื่องจาก blog นี้ไปอยู่ลำดับที่ 1 ของการค้นหา ไม่เชื่อลองดู เลยขอถือโอกาสเริ่มต้นใหม่ในช่วงปีใหม่นี้เริ่มอัพเดทบทความตั้งแต่ตอนนี้เป็นต้นไป หวังเป็นอย่างยิ่งว่าจะมีผู้ติดตามบทความของผมต่อไปน่ะครับ ปล.ท่านใดสนใจลงโฆษณาสามารถติดต่อได้ที่ hotcodephp@gmail.com
แสดงบทความที่มีป้ายกำกับ javascript แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ javascript แสดงบทความทั้งหมด

22 ส.ค. 2555

การสั่งให้ textfield จำค่าที่กรอกข้อมูลไว้ตลอด


เป็นการใช้โค้ด javascript เพื่อเรียกใช้งาน Cookie ในการจำค่าที่เรากรอกในช่อง textfield ลักษณะการทำงานของ code ก็คือเมื่อกรอกข้อมูลใน textfield แล้วกดปุ่ม submit ค่าที่เรากรอกก็จะถูกบันทึกไว้ และแสดงค่าใน textfield ตลอด ไม่ว่าเราจะ refresh กี่ครั้ง ข้อมูลก็จะยังอยู่ จนกว่าเราจะบันทึกข้อมูลใหม่ลงไปข้อมูลถึงจะเปลี่ยน โดย Cookie ตัวนี้ผมตั้งไว้ที่ 60 วันน่ะครับ ลองนำไปใช้ดู

ตัวอย่างโค้ด

<script type="text/javascript">

/***********************************************
* Remember Text INPUT field values- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

var remembervalues_days=60 //Remember text input values for how many days?
var dyncookiename=encodeURI(window.location.pathname) //Name of cookie. Changes depending on the page.

var recallinput=function(){
var cookienamevalue=rememberinput.getCookie(dyncookiename).split("##")
for (var i=0; i<cookienamevalue.length; i++){
var cookiename=cookienamevalue[i].split("#")[0]
var cookievalue=cookienamevalue[i].split("#")[1]
if (document.getElementById(cookiename)!=null && document.getElementById(cookiename).type=="text") //if this text field has a stored value
document.getElementById(cookiename).value=decodeURI(cookievalue)
}
}

var rememberinput=function(){
var rememberit=""
for (var i=0; i<arguments.length; i++){
if (document.getElementById(arguments[i]).type=="text" && document.getElementById(arguments[i]).value!="") //if this is a form text INPUT and not empty
rememberit+=arguments[i]+"#"+encodeURI(document.getElementById(arguments[i]).value)+"##"
}
rememberinput.setCookie(dyncookiename, rememberit, remembervalues_days)
}

rememberinput.getCookie=function(Name){ //get cookie value
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

rememberinput.setCookie=function(name, value, days){ //set cookie value
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+";";
}

if (window.addEventListener)
window.addEventListener("load", recallinput, false)
else if (window.attachEvent)
window.attachEvent("onload", recallinput)
else if (document.getElementById)
window.onload=recallinput

</script>
<form onSubmit="rememberinput('yourname', 'youremail')">

Name:*<br /><input type="text" id="yourname" size="25" /><br />

Email Address:*<br /><input type="text" id="youremail" size="25" /><br />

Feedback:<br />
<textarea cols="25" rows="6"></textarea>

<br />
<input type="submit" value="Submit" />

</form>
Read more >>

20 ส.ค. 2555

การทำหน้ารอระหว่างโหลดข้อมูล (preloader)

หลายท่านคงเคยเข้าเวปแล้วเจอหน้าที่บอกสถานะการโหลดหน้า webpage ก่อนที่จะเข้าหน้าเวปจริงใช่ไหมครับ วันนี้เรามาลองทำโดยใช้โค้ด javascript เข้ามาช่วย ซึ่งลักษณะการทำงานของมันก็คือ เวลาเข้าเวปจะมีรูปขึ้นระหว่างรอโหลดหน้าเวป เมื่อรอสักพักก็จะเข้าหน้าเวปจริงเลย ซึ่งมีข้อดีมันจะไม่แสดง url ที่แท้จริงของหน้าเวปหลักของเรา มาดูโค้ดตัวอย่างกันครับ

code หน้า index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Preloader example</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="max.js"></script>
</head>

<body onload="loadSite();" id="myBody">
   <div id="loader"><img src="loader.gif" border="1"/></div>
</body>

รูปตัวอย่าง



ส่วนอันนี้เป็นไฟล์ตัวย่างน่ะครับ download
Read more >>

17 ส.ค. 2555

โค้ดการจำกัดจำนวนอักษรที่จะกรอกใน textbox

เป็นโค้ด javascript ที่ใช้ในการช่วยจำกัดจำนวนตัวอักษรที่จะกรอกใน textbox กล่าวคือถ้าหากกรอกข้อความใน textbox มากกว่าที่กำหนดไว้ก็จะเปลี่ยนสี background ใน textbox เพื่อเป็นการเตือน และถ้ายังกรอกเกินอีกก็จะไม่สามารถกรอกเพิ่มเติมได้ เรามาดูตัวอย่างโค้ดกันเลยครับ

ตัวอย่าง code หน้าหลัก

<html>
<head>
<title></title>

<script type="text/javascript" src="Bs_Misc.lib.js"></script>
<script type="text/javascript" src="Bs_LimitedTextarea.class.js"></script>
<script type="text/javascript" src="Bs_ColorUtil.lib.js"></script>
<script>
function init() {
myLimitedFoo = new Bs_LimitedTextarea('foo', 50);
myLimitedFoo.setBgColorWarning();
myLimitedFoo.draw();

myLimitedBar = new Bs_LimitedTextarea('bar', 100);
myLimitedBar.setBgColorWarning(60, 'char', 'FFC0C0');
myLimitedBar.draw();
}
</script>
</head>

<body onLoad="init();">
<table border="0" cellspacing="0" cellpadding="10" width="580">
<tr>
<td valign="top">
<b>Field 1</b><br>
When 80% of the 50 chars are used, the background color starts to transform into red.<br><br>
<textarea name="foo" id="foo" cols="25" rows="4"></textarea>
</td>
<td valign="top">
<b>Field 2</b><br>
When 60 chars are used, the background color starts to transform into red.<br><br>
<textarea name="bar" id="bar" cols="25" rows="4" style="background-color:#C0FFFF;">this is a default value. this is a default value.</textarea>
</td>
</tr>
</table>

</body>
</html>

ส่วนอันนี้เป็นไฟล์ประกอบน่ะครับ download
โค้ดอาจจะเยอะหน่อยน่ะครับ ลองนำไปประยุกต์ดูครับ
Read more >>

6 ส.ค. 2555

ตัวอย่างการซ่อนหรือแสดง textbox เมื่อเปลี่ยนเงื่อนไขใน listbox

ตัวอย่างโค้ดนี้จะเป็นการใช้ javascript ในการซ่อนหรือแสดง textbox เมื่อเปลี่ยนเงื่อนไขใน listbox ซึ่งจาก code ตัวอย่างนี้เมื่อเปลี่ยนค่าใน listbox เป็น โสด ก็จะมี textbox ปรากฏขึ้นมาให้กรอก ชื่อ, นามสกุล และเลขประจำตัวประชาชน ขึ้นมา แต่ถ้าเปลี่ยนเป็นกรณีอื่น ๆ ก็จะไม่มี text box ปรากฏขึ้นมา ยังไงก็ลองนำไปประยุกต์ใช้ในการทำฟอร์มสำหรับกรอกข้อมูลได้น่ะครับ
ตัวอย่าง code 

<script>
function chksatatus(sx){
if (sx=="โสด"){
    document.getElementById('det1').style.display='none';
    document.getElementById('det2').style.display='none';
}else{
     document.getElementById('det1').style.display='block';
    document.getElementById('det2').style.display='block';
}
}
</script>
                    <select  onChange="chksatatus(this.value);" name="citizen_satatus"id="citizen_satatus">
            <option value="โสด">โสด</option>
            <option  value="สมรส"selected>สมรส</option>
            <option   value="หย่า">หย่า</option>
            <option   value="หม้าย">หม้าย</option>
          </select>

<table>

  <tr id='det1'>
    <td><div align="right">ชื่อคู่สมรส :&nbsp;</div></td>
    <td><label>
      <input name="citizen_namesamee" type="text" id="citizen_namesamee"  />
    </label></td>
    <td><div align="right">นามสกุล :&nbsp;</div></td>
    <td><label>
      <input name="citizen_surnamesamee" type="text" id="citizen_surnamesamee"  />
    </label></td>
  </tr>
  <tr id='det2'>
    <td><div align="right">รหัสบัตรประจำตัวประชาชนคู่สมรส :&nbsp;</div></td>
    <td><label>
      <input name="citizen_sameeid" type="text" id="citizen_sameeid" />
    </label></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  </table>
Read more >>

20 ก.ค. 2555

การทำ form ให้เลือกที่ radio buttom ก่อนถึงจะใส่ข้อมูลใน text field ได้

ใครเคยเขียนโค้ด php ทำ form ที่ให้ผู้ใช้งานบันทึกข้อมูลแล้วมีให้เลือกตรง radio buttom ไหมครับ แล้วเคยเจอปัญหาไหมว่าบางครั้งเราทำทั้ง radio buttom และ text field ในตัวเลือกเดียวกัน แต่ผู้ใช้งานบางคนเลือกแค่ radio buttom แต่ไม่ใส่ข้อมูลใน text field บางคนใส่ข้อมูลใน text field แต่ไม่เลือก radio buttom แล้วเราจะมีวิธีเขียน code ป้องกันยังไง ลองอ่านบทความนี้ดูครับ ก่อนอื่นดูรูปด้านล่างครับเพื่อจะได้ไม่งงกัน

คราวนี้มาดูโค้ดตัวอย่างกันครับ

<body>
<form id="form" name="form" method="post" action="add1.php" >
  <table width="100%" border="2" cellpadding="10" cellspacing="0" bordercolor="#000099">
    <tr>
      <td><strong>การดำเนินการ</strong><br />
        <input name="case1" type="radio" id="rdo2" onclick="document.form.case11.disabled=true;" value="กรณีที่ 1" />
        กรณีที่ 1
        <br />
        <input name="case1" type="radio" id="rdo3" onclick="document.form.case11.disabled=true;" value="กรณีที่ 2" />
        กรณีที่ 2<br />
        <input name="case1" type="radio" id="rdo4" onclick="document.form.case11.disabled=true;" value="กรณีที่ 3" />
        กรณีที่ 3<br />
        <input name="case1" type="radio" id="rdo5" onclick="document.form.case11.disabled=false;" value="อื่นๆ" />
        อื่น ๆ โปรดระบุ 
      <input  type="text" id="case11" size="60"  name="case11" disabled /></td>
    </tr>
  </table>
  <br />
  <p align="center">
    <input type="submit" name="Submit" value="บันทึก" />
    &nbsp;
    <input type="reset" name="Submit2" value="ยกเลิก" />
  </p>
</form>
</body>

จากโค้ดตัวคำสั่งหลักที่สำคัญก็คือตัวหนังสือสีแดงที่ใช้ onclick เข้ามาช่วย หลักการง่าย ๆ คือถ้าเราเลือกที่ช่อง กรณี 1 - กรณี 3 ช่อง อื่น ๆ โปรดระบุ ก็จะเป็น disabled แต่ถ้าเลือกที่ช่อง อื่นๆ ช่อง อื่น ๆ โปรดระบุ ก็จะเป็น abled ลองนำไปประยุกต์ใช้ดูน่ะครับ หวังว่าโค้ดตัวนี้คงเป็นประโยชน์กับทุกคน



Read more >>

19 ก.ค. 2555

การทำ jump menu เพื่อเปิดหน้าต่างใหม่


บทความ php นี้ขอพูดถึง jump menu ซึ่งเชื่อว่าหลายคนเคยใช้มากันบ้านแล้ว แต่เคยสงสัยหรือมีปัญหากันบ้างไหมว่าทำไมพอเวลาเลือกรายการใน jump menu เพื่อเปิดหน้าต่างใหม่นั้น มันจะเปิดแบบ target เป็นแบบ _blank ซึ่งบางครั้งมันจะไม่ค่อยสะดวกต่อการใช้งาน มันจะเปิดแบบ target เป็นแบบ _self ได้ไหม ก็ต้องได้สิครับเรามาดูโค้ดปกติก่อนนะครับ ผมขอเป็นแบบโค้ด php น่ะ


<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Untitled Document</title>
<script type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<form name="form1" id="form1">
  <select name="menu1" onchange="MM_jumpMenu('parent',this,0)">
   <option selected="selected">ระบุ</option>
        <option value="http://www.google.com">google</option>
  </select>
</form>
</body>

จาก code ด้านบนที่เป็นตัวสีแดงจะเป็นการทำงานในส่วนของการเปิดหน้าต่างใหม่ เราก็แก้ไขเป็นโค้ดด้านล่างนี้แทน
onchange="window.open(this.options[this.selectedIndex].value,'_blank')"
คราวนี้เราก็จะได้ jump menu ที่เปิดหน้าต่างใหม่เป็นแบบ _self แล้วครับ

Read more >>

25 พ.ค. 2555

ทำ text box ให้รับเฉพาะภาษาอังกฤษ

เป็นการทำ text box ให้ผู้ใช้งานกรอกได้เฉพาะข้อความ ภาษาอังกฤษเท่านั้น หากไม่ใช้ภาษาอังกฤษก็จะมีข้อความเตือนโค้ดด้านล่างเลยครับ


<form action="" method="POST" onsubmit="return checkeng()" name="frm_test">
<input type="text" name="test"><input type="submit" value="Submit" name="submit" />
</form>

<script language="Javascript" type="text/javascript">
function checkeng() {
  var obj=frm_test.test
  var str="abcdefghijklmnopqrstuvwxyz" //ใส่ตัวอักษรที่มีได้ลงในนี้
  var val=obj.value
  var valOK = true;
 
  for (i=0; i<val.length & valOK; i++){
    valOK = (str.indexOf(val.charAt(i))!= -1)
    alert(valOK)
  }
 
  if (!valOK) {
    alert("ภาษาอังกฤษเท่านั้น")
    obj.focus()
    return false
  } return true
}
</script>
Read more >>

การทำ popup window

เป็นการเขียนโค้ดเพื่อเปิด popup window ที่ไม่มี ทั้ง menu, toolbar และ status bar ครับ ตัวอย่างด่านล้างเลยครับจะนำไปวางโดยใช้ภาษา php หรือ html ก็ได้แต่ตัวอย่างนี้ขอเป็นโค้ด php น่ะครับ


<script>
var win = null;
function popupwindow(filename, windowname, w, h) {
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += 'resizable=0, scrollbars=0, status=0,toolbar=0, menubars=0, location=0';
  win = window.open('./'+filename, windowname, settings);
  win.window.focus();
}
</script>

อันนี้วิธีใช้ครับ

<script>popupwindow("test.php?xxx=xx", "_blank", 200, 300)</script>
Read more >>

ทำข้อความเตือนยืนยันการลบข้อมูล

การทำ form สำหรับการลบข้อมูลในฐานข้อมูล ในบางครั้งผู้ใช้งานอาจจะเผลอกดปุ่มลบไปโดยไม่รู้ตัวข้อมูลก็จะหายไปในทันที ดังนั้นจึงจำเป็นที่จะต้องมีโค้ดมาช่วยป้องกันอีกชั้น ซึ่งโค้ดก็ไม่ได้มีอะไรมากอาจจะใช้โค้ด php หรือ html ก็ได้แล้วแต่ถนัด
Code php

<form action="xxx.php">
<input type=submit name=submit value=delete onClick='return confirm()'>
</form>

<script language="JavaScript">
function confirm(num)
{
  var x=window.confirm("คุณแน่ใจ ที่จะลบรายการนี้");
  return (x);
}
</script>

ถ้าคลิกตกลงจะไปทำตามคำสั่ง action ของฟอร์มตามปกติ
Read more >>

22 พ.ค. 2555

มาทำลูกศรเลื่อนหน้าเองกัน (Page Scroller)

เป็นการใช้ลูกศรที่เราทำขึ้นเองเลื่อนหน้าเวปเราขึ้น หรือลงแทนการใช้ scrollbar สามารถนำไปประยุกต์ใช้ได้ เช่น ในกรณีที่เราทำ pop up ขึ้นมาแล้วเรากำหนดค่าไม่ให้มี scrollbar เราก็ใช้โค้ดนี้แทนได้
ตัวอย่าง code นำไปไว้ในส่วนของ body


<div id="staticbuttons" style="position:absolute;">
<a href="javascript:" onmouseover="myspeed=-thespeed" onmouseout="myspeed=0"><img
src="arrows_up.gif" border="0"></a><br>
<a href="javascript:" onmouseover="myspeed=thespeed" onmouseout="myspeed=0"><img
src="arrows_dn.gif" border="0"></a>
</div>

<script>

//Page Scroller (aka custom scrollbar)- By Dynamic Drive
//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
//This credit MUST stay intact for use

var Hoffset=70 //Enter buttons' offset from right edge of window (adjust depending on images width)
var Voffset=80 //Enter buttons' offset from bottom edge of window (adjust depending on images height)
var thespeed=3 //Enter scroll speed in integer (Advised: 1-3)

var ieNOTopera=document.all&&navigator.userAgent.indexOf("Opera")==-1
var myspeed=0

var ieHoffset_extra=document.all? 15 : 0
var cross_obj=document.all? document.all.staticbuttons : document.getElementById? document.getElementById("staticbuttons") : document.staticbuttons

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function positionit(){
var dsocleft=document.all? iecompattest().scrollLeft : pageXOffset
var dsoctop=document.all? iecompattest().scrollTop : pageYOffset
var window_width=ieNOTopera? iecompattest().clientWidth+ieHoffset_extra : window.innerWidth+ieHoffset_extra
var window_height=ieNOTopera? iecompattest().clientHeight : window.innerHeight

if (document.all||document.getElementById){
cross_obj.style.left=parseInt(dsocleft)+parseInt(window_width)-Hoffset+"px"
cross_obj.style.top=dsoctop+parseInt(window_height)-Voffset+"px"
}
else if (document.layers){
cross_obj.left=dsocleft+window_width-Hoffset
cross_obj.top=dsoctop+window_height-Voffset
}
}

function scrollwindow(){
window.scrollBy(0,myspeed)
}

function initializeIT(){
positionit()
if (myspeed!=0){
scrollwindow()
}
}

if (document.all||document.getElementById||document.layers)
setInterval("initializeIT()",20)

</script>

แล้วก็ save ไฟล้รูปภาพด่านล้างนี้ไปด้วยน่ะครับ





Read more >>

Star Rating โดยใช้ MooTools Framework

star rating script นี้เป็นการใช้ MooTools ซึ่งเป็น Javascript Framework อีกตัวหนึ่งที่น่าสนใจ สวยงาม ใช้ง่าย และมีลูกเล่นอีกมาก แต่ในบทความนี้จะกล่าวถึง STAR RATING เป็น script ที่น่านำมาประดับเว็บไซต์อีก script
ก่อนอื่นต้อง Download MooTools Framework มาไว้ก่อน โดยดาวน์โหลดได้ที่ MooTools.net
และดาวน์โหลตัวเสริม ได้ที่ MooStarRating
มาดูตัวอย่างการนำ code นี้ไปใช้กันเลยดีกว่า ก่อนอื่นต้องเรียกไฟล์ .js 2 ไฟล์ด้วยกัน ใส่ไว้ในส่วนของ tag <HEAD>


<script type="text/javascript" src="../mootools-1.3.js"></script>
<script type="text/javascript" src="Source/moostarrating.js"></script>


**** ตรวจสอบที่อยู่ไฟล์ให้ถูกต้อง ****
ตมด้วยการกำหนดค่าต่างๆ ใส่ไว้ในส่วนของ tag <HEAD> เช่นกัน


<script>

// Configure the image paths
var MooStarRatingImages = {
defaultImageFolder: 'Graphics/',
defaultImageEmpty:  'star_empty.png',
defaultImageFull:   'star_full.png',
defaultImageHover:  "star_boxed_hover.png"
};

// Post iD
var postId = 10;

// When the DOM is ready....
window.addEvent("domready",function() {

// Create our instance
// Advanced options
var advancedRating = new MooStarRating({
form: 'ratingsForm',
radios: 'rating',
half: true,
//imageEmpty: 'star_boxed_empty.png',
//imageFull:  'star_boxed_full.png',
//imageHover: "star_boxed_hover.png",
width: 17,
tip: 'Rate <I>[VALUE] / 10.0</I>',
tipTarget: $('htmlTip'),
tipTargetType: 'html',
click: function(value) {
// Send ajax request to server
new Request.send({
url: "rating.php",
data: { rating: value, postId: postId }
});
}
});



});

</script>


หลังจากนั้นมาสร้าง Form จะใช้โค้ด php หรือ html ก็ได้แล้วแต่ความถนัด


<form name="ratingsForm">
    <label>Do you like this article?</label>
    <input type="radio" name="rating" value="0.5">
    <input type="radio" name="rating" value="1.0">
    <input type="radio" name="rating" value="1.5">
    <input type="radio" name="rating" value="2.0">
    <input type="radio" name="rating" value="2.5">
    <input type="radio" name="rating" value="3.0">
    <input type="radio" name="rating" value="3.5">
    <input type="radio" name="rating" value="4.0">
    <input type="radio" name="rating" value="4.5">
    <input type="radio" name="rating" value="5.0">
    <input type="radio" name="rating" value="5.5" checked="checked">
    <input type="radio" name="rating" value="6.0">
    <input type="radio" name="rating" value="6.5">
    <input type="radio" name="rating" value="7.0">
    <input type="radio" name="rating" value="7.5">
    <input type="radio" name="rating" value="8.0">
    <input type="radio" name="rating" value="8.5">
    <input type="radio" name="rating" value="9.0">
    <input type="radio" name="rating" value="9.5">
    <input type="radio" name="rating" value="10.0">
  <span id="htmlTip"></span>
</form>





Read more >>

21 พ.ค. 2555

Facebook Lightbox การทำ pop-up แบบ Facebook

การทำ pop-up ตามสไตล์แบบ Facebook โดยใช้ javascript framework Mootools
ก่อนอื่นต้องทำการโหลด javascript framework ที่ชื่อว่า Mootools มาไว้ก่อนโดยโหลดได้ที่ MooTools.net
และดาวน์โหลตัวเสริม ได้ที่ LightFace
หลังจากนั้นมาเริ่มต้นสร้าง Pop-up สไตล์ Facebook ด้วยการเรียกไฟล์ .js 2 ไฟล์ด้วยกัน เป็นไฟล์หลักของ MooTools framework กับอีกไฟล์เป็นไฟล์ที่ใช้สำหรับทำ pop-up facebook style ดังตัวอย่าง ใส่ไว้ในส่วนของ tag <HEAD>
<script type="text/javascript" src="../mootools-1.3.js"></script>
<script type="text/javascript" src="Source/LightFace.js"></script>
และทำการ import css ไฟล์
<style type="text/css">
@import "Assets/LightFace.css";
</style>
**** ตรวจสอบที่อยู่ไฟล์ให้ถูกต้อง ****
ทำการกำหนดค่าเริ่มต้นของ Script โดยกำหนดความกว้าง ความสูง ชื่อ และเนื้อหา
ทำการกำหนดปุ่มสำหรับปิดหน้าต่าง pop-up facebook ของเรา

code javascript

<script type="text/javascript"> 
 window.addEvent('domready',function() {
  // Create instance
  var modal = new LightFace({
   height: 200,
   width: 300,
   title: 'My Profile,
   content: 'Lorem ipsum....',
   buttons: [
     { title: 'Close', event: function() { this.close(); }, color: 'green' }
    ]
   });
  $('profile').addEvent('click',function() { modal.open(); }); //ทำการกำหนดให้แสดง pop up เมื่อมีการคลิก ID="profile"
});
<script>

ในส่วนของ HTML เพียงแค่กำหนด ID ให้กับลิ้งค์ที่จะเรียก pop up ขึ้นมา 
<a href="javascript:;" id="profile">Facebook Lightbox </a>
เพียงเท่านี้ก็เสร็จสิ้นการทำ POP-UP ตามสไตล์ของ FACEBOOK แล้วครับ

Read more >>

Upper / Lower case พิมพ์อักษรตัวเล็ก / ตัวใหญ่


ในการทำแบบฟอร์มของเว็บไซต์ บางครั้งเราก็ต้องการให้ user ใส่เป็นตัวอักษรตัวพิมพ์ใหญ่ทั้งหมด เพื่อต้องการให้อ่านง่าย หรือต้องการให้ใส่เป็นตัวพิมพ์เล็กทั้งหมด การใช้ javascript ก็เป็นอีกหนึ่งทางเลือกที่น่าสนใจ วิธีทำก็ง่ายๆ เพียงสร้าง form ขึ้นมาจะใช้โคัด php ,html ก็ได้แล้วนำ code ด่านล้างไปใส่

ตัวอย่างcode Javascript เปลี่ยนเป็นตัวพิมพ์ใหญ่ (Upper case) กันเลยดีกว่า

<form name="capsform">
<input type="text" name="caps" size=40 value="" onChange="javascript:this.value=this.value.toUpperCase();">
<br>
<input type="button" value="Ok!">
</form>

อีกหนึ่งตัวอย่าง code Javascript ตัวพิมพ์เล็ก (Lower case)
<form name="capsform">
<input type="text" name="caps" size=40 value="" onChange="javascript:this.value=this.value.toLowerCase();">
<br>
<input type="button" value="Ok!">
</form>
Read more >>

18 พ.ค. 2555

วิธีแก้ไข pop up ถูกบล็อค

<script type="text/javascript">
<!--
var answer = prompt ("What is your name ?","");
alert ("Hello there, " + answer);
// -->
</script>
จาก code ข้างบนหากเขียน javascript prompt แบบธรรมดา ในปัจจุบันอาจจะต้องถูก block อย่างแน่นอน เนื่องด้วยปัจจุบัน Browser ต่างๆ ล้วนมีระบบรักษาความปลอดภัยอย่างเข้มงวดมากขึ้น ทำอย่างไรดี วันนี้มีทางแก้ไข
ผลลัพธ์ที่ได้จากการเขียน Javascript prompt แบบปกติ แล้วลอง run script นี้ทาง browser คือ
"This website is using a scripted window to ask you for information. If you trust this website, click here to allow scripted windows..."

ตัวอย่างโค้ด
code javascript
<script type="text/javascript">
// This is variable for storing callback function
var ae_cb = null;
// this is a simple function-shortcut
// to avoid using lengthy document.getElementById
function ae$(a) { return document.getElementById(a); }
// This is a main ae_prompt function
// it saves function callback
// and sets up dialog
function ae_prompt(cb, q, a) {
ae_cb = cb;
ae$('aep_t').innerHTML = document.domain + ' question:';
ae$('aep_prompt').innerHTML = q;
ae$('aep_text').value = a;
ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = '';
ae$('aep_text').focus();
ae$('aep_text').select();
}
// This function is called when user presses OK(m=0) or Cancel(m=1) button
// in the dialog. You should not call this function directly.
function ae_clk(m) {
// hide dialog layers
ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = 'none';
if (!m)
ae_cb(null); // user pressed cancel, call callback with null
else
ae_cb(ae$('aep_text').value); // user pressed OK
}
</script>

ต่อไปเป็นส่วนของ CSS เป็นการทำให้แสดง prompt เพื่อใส่ข้อมูล
<style type="text/css">
#aep_ovrl {
background-color: black;
-moz-opacity: 0.7; opacity: 0.7;
top: 0; left: 0; position: fixed;
width: 100%; height:100%; z-index: 99;
}
#aep_ww { position: fixed; z-index: 100; top: 0; left: 0; width: 100%; height: 100%; text-align: center;}
#aep_win { margin: 20% auto 0 auto; width: 400px; text-align: left;}
#aep_w {background-color: white; padding: 3px; border: 1px solid black; background-color: #EEE;}
#aep_t {color: white; margin: 0 0 2px 3px; font-family: Arial, sans-serif; font-size: 10pt;}
#aep_text {width: 100%;}
#aep_w span {font-family: Arial, sans-serif; font-size: 10pt;}
#aep_w div {text-align: right; margin-top: 5px;}
</style>
<!-- IE specific code: -->
<!--[if lte IE 7]>
<style type="text/css">
#aep_ovrl {
position: absolute;
filter:alpha(opacity=70);
top: expression(eval(document.body.scrollTop));
width: expression(eval(document.body.clientWidth));
}
#aep_ww {
position: absolute;
top: expression(eval(document.body.scrollTop));
}
</style>
<![endif]-->

และสุดท้าย เป็นการเรียกใช้ ae_prompt ในส่วนของ HTML. ต้องใส่ให้อยู่ภายใจ tag <body>
สามารถนำไปประยุกต์ใช้ทั้งโค้ด php หรือ โค้ด html
<!-- ae_prompt HTML code -->
<div id="aep_ovrl" style="display: none;">&nbsp;</div>
<div id="aep_ww" style="display: none;">
<div id="aep_win"><div id="aep_t"></div>
<div id="aep_w"><span id="aep_prompt"></span>
<br /><input type="text" id="aep_text" onKeyPress=
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);">
"if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);">
<br><div><input type="button" id="aep_ok" onclick="ae_clk(1);" value="OK">
<input type="button" id="aep_cancel" onclick="ae_clk(0);" value="Cancel">
</div></div>
</div>
</div>
<!-- ae_prompt HTML code -->

ส่วนอันนี้เป็นตัวอย่างการนำไปใช้งาน
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0047)http://www.anyexample.com/files/js/prompt2.html -->
<HTML><HEAD><TITLE>prompt test</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-874">
<SCRIPT type=text/javascript>

function hw1()
{
    ae_prompt( hw2, 'คุณชื่ออะไรครับ?', 'ผู้ไม่ประสงค์ออกนาม');
}

function hw2(n)
{
    var hello = document.getElementById('hello');
    hello.innerHTML = '<h1>สวัสดีครับ, คุณ' + n + '!</h1>';
}
// ae_prompt function sources
var ae_cb = null;
function ae$(a) { return document.getElementById(a); }
function ae_prompt(cb, q, a) {
ae_cb = cb;
ae$('aep_t').innerHTML = document.domain + ' คำถามวันนี้:';
ae$('aep_prompt').innerHTML = q;
ae$('aep_text').value = a;
ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = '';
ae$('aep_text').focus();
ae$('aep_text').select();
}
function ae_clk(m) {
ae$('aep_ovrl').style.display = ae$('aep_ww').style.display = 'none';
if (!m) ae_cb(null); else ae_cb(ae$('aep_text').value);
}
// ae_prompt function sources
</SCRIPT>
<!-- CSS styles for ae_prompt function -->
<STYLE type=text/css>#aep_ovrl {
Z-INDEX: 99; LEFT: 0px; WIDTH: 100%; POSITION: fixed; TOP: 0px; HEIGHT: 100%; BACKGROUND-COLOR: black; -moz-opacity: 0.7; opacity: 0.7
}
#aep_ww {
Z-INDEX: 100; LEFT: 0px; WIDTH: 100%; POSITION: fixed; TOP: 0px; HEIGHT: 100%; TEXT-ALIGN: center
}
#aep_win {
MARGIN: 20% auto 0px; WIDTH: 400px; TEXT-ALIGN: left
}
#aep_w {
BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 3px; BORDER-TOP: black 1px solid; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; BORDER-LEFT: black 1px solid; PADDING-TOP: 3px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #eee
}
#aep_t {
FONT-SIZE: 10pt; MARGIN: 0px 0px 2px 3px; COLOR: white; FONT-FAMILY: Arial, sans-serif
}
#aep_text {
WIDTH: 100%
}
#aep_w SPAN {
FONT-SIZE: 10pt; FONT-FAMILY: Arial, sans-serif
}
#aep_w DIV {
MARGIN-TOP: 5px; TEXT-ALIGN: right
}
</STYLE>
<!-- IE specific code: --><!--[if lte IE 7]>
<STYLE type=text/css>#aep_ovrl {
FILTER: alpha(opacity=70); ; WIDTH: expression(eval(document.body.clientWidth)); POSITION: absolute; ; TOP: expression(eval(document.body.scrollTop))
}
#aep_ww {
POSITION: absolute; ; TOP: expression(eval(document.body.scrollTop))
}
</STYLE>
<![endif]--><!-- CSS styles for ae_prompt function -->
<META content="MSHTML 6.00.6000.16640" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff><!-- ae_prompt HTML code -->
<DIV id=aep_ovrl style="DISPLAY: none"></DIV>
<DIV id=aep_ww style="DISPLAY: none">
<DIV id=aep_win>
<DIV id=aep_t></DIV>
<DIV id=aep_w><SPAN id=aep_prompt></SPAN><BR><INPUT
onkeypress="if((event.keyCode==10)||(event.keyCode==13)) ae_clk(1); if (event.keyCode==27) ae_clk(0);"
id=aep_text><BR>
<DIV><INPUT id=aep_ok onclick=ae_clk(1); type=button value=OK> <INPUT id=aep_cancel onclick=ae_clk(0); type=button value=Cancel>
</DIV></DIV></DIV></DIV><!-- ae_prompt HTML code -->
<DIV id=hello>สวัสดี! <SPAN
style="CURSOR: pointer; COLOR: blue; TEXT-DECORATION: underline"
onclick=hw1();>คลิกที่นี่</SPAN> เพื่อใส่ชื่อของคุณ. </DIV></BODY></HTML>
Read more >>

17 เม.ย. 2555

ขยายหน้าจอให้เป็นแบบ Maximize โดยอัตโนมัติ

ตามโค้ดด้านล่างนี้เลยครับ

<script language="JavaScript1.2">
<!--
top.window.moveTo(0,0);
if (document.all) {
top.window.resizeTo(screen.availWidth,screen.availHeight);
}
else if (document.layers||document.getElementById) {
if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
top.window.outerHeight = screen.availHeight;
top.window.outerWidth = screen.availWidth;
}
}
//-->
</script>
Read more >>

ป้องกันการคลิกขวา (No right click) ให้กับรูปภาพ

เพียงแค่นำโค้ดด้านล่างนี้ไปไว้ก่อนที่จะปิด </body>

<script language="JavaScript1.2">
var clickmessage="Right click disabled on images!"

function disableclick(e) {
if (document.all) {
if (event.button==2||event.button==3) {
if (event.srcElement.tagName=="IMG"){
alert(clickmessage);
return false;
}
}
}
else if (document.layers) {
if (e.which == 3) {
alert(clickmessage);
return false;
}
}
else if (document.getElementById){
if (e.which==3&&e.target.tagName=="IMG"){
alert(clickmessage)
return false
}
}
}

function associateimages(){
for(i=0;i<document.images.length;i++)
document.images[i].onmousedown=disableclick;
}

if (document.all)
document.onmousedown=disableclick
else if (document.getElementById)
document.onmouseup=disableclick
else if (document.layers)
associateimages()
</script>
Read more >>

12 เม.ย. 2555

แสดงข้อมูลความเตือนเมื่อออกจากเพจ

แสดงกรอบโต้ตอบเมื่อเราปิดบราวเซอร์ ออกจากเพจ เปลี่ยนเพจ หรือ กระทั่ง Refresh โดยใช้ event onunload ของ body อีเวนต์นี้ อาจทำงานไม่เหมือนกันในแต่ละ browser นะครับ

<body onunload="dounload()" bgcolor="#FFFFFF">
</body>
<script language="Javascript" type="text/javascript">
function dounload () {
  alert("ขอบคุณ")
}
</script>
Read more >>

26 มี.ค. 2555

การเปิดหน้าเวปใหม่ภายใน frame

โค้ดตามนี้เลย

<form name="jumpy">
<select name="example" size="1" onChange="gone()">
<!-- CHANGE THE BELOW URLS TO YOUR OWN-->
<option value="http://www.yahoo.com" selected>Yahoo.com</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.lycos.com">Lycos</option>
<option value="http://www.AltaVista.com">AltaVista</option>
</select>

<input type="button" name="test" value="Go!" onClick="gone()">
</form>

<script language="javascript">
<!--

//Drop-down Document Viewer- © Dynamic Drive (www.dynamicdrive.com)
//For full source code, 100's more DHTML scripts, and TOS,
//visit http://www.dynamicdrive.com

//Specify display mode (0 or 1)
//0 causes document to be displayed in an inline frame, while 1 in a new browser window
var displaymode=1
//if displaymode=0, configure inline frame attributes (ie: dimensions, intial document shown
var iframecode='<iframe id="external" style="width:95%;height:400px" src="http://www.yahoo.com"></iframe>'

/////NO NEED TO EDIT BELOW HERE////////////

if (displaymode==0)
document.write(iframecode)

function gone(){
var selectedurl=document.jumpy.example.options[document.jumpy.example.selectedIndex].value
if (document.getElementById&&displaymode==0)
document.getElementById("external").src=selectedurl
else if (document.all&&displaymode==0)
document.all.external.src=selectedurl
else{
if (!window.win2||win2.closed)
win2=window.open(selectedurl)
//else if win2 already exists
else{
win2.location=selectedurl
win2.focus()
}
}
}
//-->
</script>
Read more >>

การแสดงคำอธิบายในฟอร์ม


Username: [?]
Password:   [?]

จากตัวอย่างด้านบนหากเราเอาเมาส์ไปชี้ที่เครื่องหมาย ? ก็จะมีคำอธิบายขึ้นมา
โค้ด
เอาไปวางในส่วนของ head
<style type="text/css">

#hintbox{ /*CSS for pop up hint box */
position:absolute;
top: 0;
background-color: lightyellow;
width: 150px; /*Default width of hint.*/ 
padding: 3px;
border:1px solid black;
font:normal 11px Verdana;
line-height:18px;
z-index:100;
border-right: 3px solid black;
border-bottom: 3px solid black;
visibility: hidden;
}

.hintanchor{ /*CSS for link that shows hint onmouseover*/
font-weight: bold;
color: navy;
margin: 3px 8px;
}

</style>

<script type="text/javascript">

/***********************************************
* Show Hint script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
var horizontal_offset="9px" //horizontal offset of hint box from anchor link

/////No further editting needed

var vertical_offset="0" //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
}
else{
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
if ((ie||ns6) && document.getElementById("hintbox")){
dropmenuobj=document.getElementById("hintbox")
dropmenuobj.innerHTML=menucontents
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
dropmenuobj.style.visibility="visible"
obj.onmouseout=hidetip
}
}

function hidetip(e){
dropmenuobj.style.visibility="hidden"
dropmenuobj.style.left="-500px"
}

function createhintbox(){
var divblock=document.createElement("div")
divblock.setAttribute("id", "hintbox")
document.body.appendChild(divblock)
}

if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)
window.onload=createhintbox

</script>
ส่วนอันนี้เป็นส่วนของ form
<form>
<b>Username:</b> <input type="text" class="test" /> <a href="#" class="hintanchor" onMouseover="showhint('Please choose a username. Should consist of alphanumeric characters only.', this, event, '150px')">[?]</a><br />
<b>Password:</b>  <input type="text" class="test" /> <a href="#" class="hintanchor" onMouseover="showhint('Enter the desired password. <b>Must</b> be 8 characters or longer.', this, event, '200px')">[?]</a><br />
</form>
Read more >>

15 มี.ค. 2555

การใส่ comma ในช่อง textbox ที่เป็นตัวเลข


<script type="text/javaScript">
//เติม , (คอมมา)
function dokeyup( obj )
{
  var key = event.keyCode;
  if( key != 37 & key != 39 & key != 110 )
  {
    var value = obj.value;
    var svals = value.split( "." ); //แยกทศนิยมออก
    var sval = svals[0]; //ตัวเลขจำนวนเต็ม
 
    var n = 0;
    var result = "";
    var c = "";
    for ( a = sval.length - 1; a >= 0 ; a-- )
    {
      c = sval.charAt(a);
      if ( c != ',' )
      {
        n++;
        if ( n == 4 )
        {
          result = "," + result;
          n = 1;
        };
        result = c + result;
      };
    };
 
    if ( svals[1] )
    {
      result = result + '.' + svals[1];
    };
 
    obj.value = result;
  };
};

//ให้ text รับค่าเป็นตัวเลขอย่างเดียว
function checknumber()
{
  key = event.keyCode;
  if ( key != 46 & ( key < 48 || key > 57 ) )
  {
    event.returnValue = false;
  };
};
</script>

<input name="text1" type="text" value='1200' onkeyup="dokeyup(this);" onchange="dokeyup(this);" onkeypress="checknumber()" size="30" maxlength="30">
Read more >>