'JavaScript'에 해당되는 글 3건

  1. 2008/07/15 Checkbox
  2. 2008/06/19 JS용 SortManager
  3. 2007/09/20 External JavaScript and PHP

Checkbox

JAVASCRIPT 2008/07/15 18:32 |

자바스크립트로 DOM으로 객체를 생성해서 checkbox를 만드는 과정에서 30분정도를 ...헤맷다...
(참고 : 상황설명을 위해...실제 코드가 아닌 테스트성 코드를 적습니다.)

내머릿속의 코드는 이랬다..
var box = document.createElement("input");
box.setAttribute("type","checkbox");
box.setAttribute("value","Y");
box.setAttribute("checked","checked");

 이렇게 코드를 만들면 체크된 checkbox를 만날 수 있으리라..아무 생각없이 했는데 체크박스는 있는데..
체크는 되어 있지 않았다.. 뜨헉..

그래서 생성된 HTML을 들여다 봤더니..checked라는 속성자체가 없었다...
그래서 트릭으로 한 것은?

var dummy = new Element("div");
var str = "";
dummy.adopt(box);
str = dummy.innerHTML.replace(new RegExp("checkbox",gi),"checkbox checked"));
dummy.remove();

위 처럼해서 html string을 구한다음...innerHTML로 넣어 줬다...쩝..
checked구현할려고 별짓을 다 했다..
머리속으로 왜 안되지? 이상하군...이런 생각만 했는데..

너무 이상해서 checkbox의 property를 뒤지던 중..
defaultChecked를 발견...헉..이건 모지?  default....그렇다 default다..

그래서 기존에 작성했던 소스 파기~
아래 처럼 변경.

var box = new Element("input");
box.setAttribute("type","checkbox");
box.setAttribute("value","Y");
box.setAttribute("defaultChecked","checked");

잘된다...하루에도 몇개의 언어를 왔다 갔다 하니 머리도 왔다 갔다 하나부다....ㅠ_ㅠ
크리에이티브 커먼즈 라이선스
Creative Commons License

'JAVASCRIPT' 카테고리의 다른 글

에잇! 사라져 버려 IE 라디오버튼 생성 오류 by Javascript  (8) 2008/08/14
Checkbox  (0) 2008/07/15
[펌]AJAX로 SOAP 웹 서비스 사용하기  (0) 2008/07/04
JS용 SortManager  (0) 2008/06/19
자바스크립트 압축기  (2) 2008/05/21
XMLHttpRequest  (0) 2008/05/21
Open Editor  (3) 2008/05/09
상식  (2) 2008/03/28

Trackback Address :: http://lovedev.tistory.com/trackback/281

댓글을 달아 주세요

JS용 SortManager

JAVASCRIPT 2008/06/19 16:32 |
Array인데 Object를 가진 Array의 경우 array.sort()의 결과를 얻기 뒤해서
소팅 걸때 쓸려고 만들어 봤다...

var SortManager = {
  type : "String",
  target : "",
  toLowerString : function(a,b){
   var x = a[SortManager.target] ? a[SortManager.target].toLowerCase() : "";
   var y = b[SortManager.target] ? b[SortManager.target].toLowerCase() : "";
   return [x,y];
  },
  ASC : function(a,b){
   var x = "" , y ="";
   try{
    switch(SortManager.type){
     case "String" :
      var res = SortManager.toLowerString(a,b);
      x = res[0];
      y = res[1];
     break;
     default :
      x = a[SortManager.target];
      y = b[SortManager.target];
     break;
    }
   }catch(e){}
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  },
  DESC : function(a,b){
   var x = "" , y ="";
   try{
    switch(SortManager.type){
     case "String" :
      var res = SortManager.toLowerString(a,b);
      x = res[0];
      y = res[1];
     break;
     default :
      x = a[SortManager.target];
      y = b[SortManager.target];
     break;
    }
   }catch(e){}
   return ((x > y) ? -1 : ((x < y) ? 1 : 0));
  },
  RAND : function(a,b){
   return (Math.random()*10)*(Math.random() - .5);
  }
 };

사용법
var a = [{n:"2004.12.18",b:"b"},{n:"2008.12.14",b:"a"},{n:"2008.120.14",b:"a"}];
SortManager.type = "String";
SortManager.target = "n";

alert(a.sort(SortManager.DESC));
만드는 김에 Random Sorting도 만들어 봤다..

alert(a.sort(SortManager.RAND));
식으로 사용하면 된다.

ActionScript도 이와 비슷하게 구현이 가능하다..
예전에 이에 대해 적어 놓은 글이 있다.
2007/07/09 - [ACTIONSCRIPT] - [3.0]Using Custom Function Array random sort


크리에이티브 커먼즈 라이선스
Creative Commons License

'JAVASCRIPT' 카테고리의 다른 글

에잇! 사라져 버려 IE 라디오버튼 생성 오류 by Javascript  (8) 2008/08/14
Checkbox  (0) 2008/07/15
[펌]AJAX로 SOAP 웹 서비스 사용하기  (0) 2008/07/04
JS용 SortManager  (0) 2008/06/19
자바스크립트 압축기  (2) 2008/05/21
XMLHttpRequest  (0) 2008/05/21
Open Editor  (3) 2008/05/09
상식  (2) 2008/03/28

Trackback Address :: http://lovedev.tistory.com/trackback/256

댓글을 달아 주세요

AJAX가 없던시절....이걸 섰었지..

External JavaScript and PHP

One of the lesser known sides of external JavaScript is the ability to reference a PHP file instead of the familiar .js file. At first such an notion may seem strange, even impossible; after all, who among us isn't familiar with that barrier dividing server side and client side scripts that prohibit the two from interacting? Well, it turns out superficial exchange is allowed. Using external JavaScript, you'll see how PHP and JavaScript can work together in a way you may not have thought possible, and to the great benefit of JavaScript.

The syntax

The syntax to referencing a PHP file using external JavaScript is consistent enough with what we already know:

<script type="text/javascript" src="myscript.php"></script>

where "myscript.php" is either an absolute or relative path to a PHP script instead of the usual .js file. You can even pass parameters to the PHP script through the URL string:

<script type="text/javascript" src="myscript.php?id=3&name=george"></script>

Your PHP script can then get to these parameters using the global variable $HTTP_GET_VARS[]. So you're probably wondering at this point: "So what's the catch?" Well, there is no catch really, just a few limitations. Since we are invoking the PHP script indirectly and via JavaScript, the final output of the PHP script needs to be valid JavaScript. Think of it as a dynamic .js file, bounded by the same limitations as a regular .js file. A normal PHP script called inside a PHP page can output raw HTML and modify the source code of the page. The JavaScript invoked version obviously cannot, but don't worry, there's plenty of what it can do.

Here's a basic example of a PHP script- ip.php- being called by external JavaScript to do something that JavaScript alone cannot:

<?
//"ip.php" example- display user IP address on any page
Header("content-type: application/x-javascript");
$serverIP=$_SERVER['REMOTE_ADDR'];
echo "document.write(\"Your IP address is: <b>" . $serverIP . "</b>\")";
?>

And once called by external

<script type="text/javascript" src="ip.php"></script>
크리에이티브 커먼즈 라이선스
Creative Commons License

'JAVASCRIPT' 카테고리의 다른 글

execCommand 명령어 목록  (0) 2008/01/07
[JS] caller를 이용한 factory , singletone 패턴구현  (0) 2007/12/17
//OPERA/IE/FF 즐겨찾기추가..  (0) 2007/10/09
XMLHttpRequest and Struts  (0) 2007/09/20
External JavaScript and PHP  (0) 2007/09/20
createTextRange  (0) 2007/07/23
객체를 카피하자...FOR IE  (0) 2007/07/23
Firebug로 profiling뜨기..  (0) 2007/07/23

Trackback Address :: http://lovedev.tistory.com/trackback/68

댓글을 달아 주세요