'팁&테크/Javascript'에 해당되는 글 19건
- 2010.11.25 자바스크립트 문자열 byte 단위 자르기 및 byte 구하기 1
- 2007.07.03 PHP Sleep 함수 구현
- 2007.06.29 Ajax 한글전송
- 2007.06.29 iframe 페이지 크기에 맞게 자동 resize
- 2007.06.29 iframe 자동크기조절
- 2007.06.29 php 함수를 자바스크립트로
- 2007.06.29 자바스크립트로 쿠키 처리
- 2007.06.29 팝업창 화면 크기에 맞게 띄우기
- 2007.06.29 팝업창 화면 중앙에 띄우기
자바스크립트 문자열 byte 단위 자르기 및 byte 구하기
var length = 0;
return length;
PHP Sleep 함수 구현
<script language="JavaScript"> <!-- function sleep(sec) { var now = new Date(); var exitTime = now.getTime() + (sec*1000); while (true) { now = new Date(); if (now.getTime() > exitTime) return; } } sleep(5); alert('메롱'); //--> </script>
출처 : PHPSCHOOL 지우님
Ajax 한글전송
encodeURIComponent( string ) --> rawurldecode( iconv( "UTF-8", "CP949", $string ) )
PHP 에서 보내고 --> JavaScript 받을때
rawurlencode( iconv( "CP949", "UTF-8", $string ) ) --> decodeURIComponent( string )
iframe 페이지 크기에 맞게 자동 resize
function voidResizeFrame(){ //페이지가 로딩되면 바로 리사이즈..
var h = parseInt(document.body.scrollHeight);
self.resizeTo(document.body.scrollWidth + (document.body.offsetWidth-document.body.clientWidth), h);
}
voidResizeFrame();
iframe 페이지 내에 넣어둠
iframe 자동크기조절
[소스: frame.html]
<script>
function syncHeight(el) {
el = typeof el == 'string' ? document.getElementById(el) : el;
el.setExpression('height','contentWindow.document.body.scrollHeight+5');
}
</script>
<iframe name="test" id="test" src="content.html" on-Load="syncHeight(this)"></iframe>
[/소스]
[소스:content.html]
...
이것은 iframe 내용입니다.<br>
이것은 iframe 내용입니다.<br>
이것은 iframe 내용입니다.<br>
이것은 iframe 내용입니다.<br>
<button o-nClick="this.outerHTML='이것은 추가된내용입니다<br>'+this.outerHTML">누르면 내용이 추가됩니다</button>
[/소스]
php 함수를 자바스크립트로
자바스크립트용으로 PHP의 함수들을 흉내내본 것입니다. 더 필요한 게 있으면 그때그때 추가할 예정입니다. 기능 건의 받습니다. -_- v prototype 으로 정의되어있어서... ("<b>test</b>").stripTags(); 혹은 var str = " !!!! "; str = str.trim(); 과 같이 사용가능합니다. <script language="javascript" type="text/javascript"> // HTML 특수문자를 변환 String.prototype.htmlChars = function () { var str = ((this.replace('"', '&')).replace('"', '"')).replace('\'', '''); return (str.replace('<', '<')).replace('>', '>'); } // 좌우 공백없애는 함수 String.prototype.trim = function () { return this.replace(/(^s*)|(s*$)/g, ""); } // 왼쪽 공백없애는 함수 String.prototype.ltrim = function () { return this.replace(/^s*/g, ""); } // 오른쪽 공백없애는 함수 String.prototype.rtrim = function () { return this.replace(/s*$/g, ""); } // 태그만 제거 String.prototype.stripTags = function () { var str = this; var pos1 = str.indexOf('<'); if (pos1 == -1) return str; else { var pos2 = str.indexOf('>', pos1); if (pos2 == -1) return str; return (str.substr(0, pos1) + str.substr(pos2+1)).stripTags(); } } // 대소문자 구별하지 않고 단어 위치 찾기 String.prototype.ipos = function (needle, offset) { var offset = (typeof offset == "number")?offset:0; return str.toLowerCase().indexOf(needle.toLowerCase(), offset); } // 대소문자 구별하지 않고 뒤에서부터 단어위치 찾기 String.prototype.ripos = function (needle, offset) { var offset = (typeof offset == "number")?offset:0; return str.toLowerCase().lastIndexOf(needle.toLowerCase(), offset); } // 문자열을 배열로 String.prototype.toArray = function () { var len = this.length; var arr = new Array; for (var i=0; i<len; i++) arr[i] = this.charAt(i); return arr; } </script>
자바스크립트로 쿠키 처리
<script language="javascript" type="text/javascript"> function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document_.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document_.cookie.substring(i, j) == arg) return getCookieVal (j); i = document_.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie (name, value) { var argv = SetCookie.arguments; var argc = SetCookie.arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : null; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; if(expire) { todayDate.setDate(todayDate.getDate() + expires); } document_.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + todayDate.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); } function DeleteCookie (name) { var exp = new Date(); exp.setTime (exp.getTime() - 1); var cval = GetCookie (name); document_.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); } </script>
팝업창 화면 크기에 맞게 띄우기
<script language="javascript" type="text/javascript"> function resizeWindow(){ if(document.all) { var x = (document.body.scrollWidth > 1000) ? 1000 : document.body.scrollWidth; var y = (document.body.scrollHeight > 700) ? 700 : document.body.scrollHeight; x -= document.body.clientWidth; y -= document.body.clientHeight; window.resizeBy(x, y); } } resizeWindow(); </script>
팝업창 화면 중앙에 띄우기
<script language="javascript" type="text/javascript"> function MoveCenterWindow(){ var cx = document.body.clientWidth; var cy = document.body.clientHeight; var sw = screen.availWidth; var sh = screen.availHeight; var px = (sw-cx)/2; var py = (sh-cy)/2 window.moveTo(px, py); } MoveCenterWindow(); </script>