javascript 한글 영문 포함 문자 길이 간단히 알아내기

js API/javascript 2012. 5. 8. 11:06

한글과 영문이 섞인 문장의 길이를 간단히 체크하는 스크립트입니다.


기본적으로 확장은 어떻게든 가능한 것이겠죠?

기본적인 길이 체크 하는 소스입니다.


< script>
function chkStrLength(str) {
	var str;
	var han_count=0;

	han_count = (escape(str)+"%u").match(/%u/g).length-1;

return (str.length + han_count);
}
< /script>



javascript 현재 접속 주소(document.location.href) params 값과 원하는 데이타만 가져오기

js API/javascript 2012. 5. 4. 17:17

javascript 로 현재 접속 URL 을 알아 낼 수 있는 스크립트 함수가

document.location.href 와 document.URL 이 있다. 이 주소로 현재 주소를 불러온다.


< script>
// http://apmsoft.tistory.com/test.php?gid=123&n=000
var this_url = document.location.href;
< /script>



#@  gid 값만 가져 오기


< script>
function parseUrl( url ) {
    var a = document.createElement('a');
    a.href = url;
    return a;
}

// ?gid=123&n=000
var s = parseUrl(lux_gaq[i]).search;

if(s.match(/gid/)){
	var id = s.match(/gid=([^&]+)/)[1];
	
	// gid=123
	alert('gid='+id); 
}		

< /script>



@ 실습 하기 /===============================================================



< script>
var lux_drf=(document.referrer)?encodeURIComponent(document.referrer):encodeURIComponent(parent.document.referrer);
var lux_page=(document.location)?encodeURIComponent(document.location):encodeURIComponent(document.URL);
var lux_gaq=lux_gaq||[];
lux_gaq.push(['MjNFNzhNUFE5WDE%3D']);
lux_gaq.push([lux_drf]);
lux_gaq.push([lux_page]);
(function(){
	var lux_ga=document.createElement('script');lux_ga.type='text/javascript';lux_ga.async=true;
	lux_ga.src=('https:'==document.location.protocol?'https://ssl':'http://')+'test.com/ga.js';
	var lux_gas=document.getElementsByTagName('script')[0];lux_gas.parentNode.insertBefore(lux_ga,lux_gas);
})();
< /script>



// test.com/gajs

function parseUrl( url ) {
    var a = document.createElement('a');
    a.href = url;
    return a;
}

// 주소 : http://test.com/ga.html?kid=NEY2MExOTzFYNDM%3D
var lux_params='';
var lux_ak=Array('gid','kid','mid');
for(var i=0;i



device (디바이스) 체크 및 종류 알아내기

js API/javascript 2012. 4. 10. 11:35

device (디바이스) 체크 및 종류 알아내기


1. JavaScript로 device (디바이스) 체크 및 종류 알아내기

각종 프로그래밍 언어 PHP, JSP등등으로 알아낼 수도 있지만
이건 자바스크립트로 접속 단말기를 알아내는 방법입니다.

함께 사용하면 더 편리합니다.




@ 디바이스 정보 알아내는 스크립트 function.js /================

< script >
//@ return String (device name)
function check_device(){
	var mobileKeyWords = new Array('iPhone', 'iPod', 'BlackBerry', 'Android', 'Windows CE', 'LG', 'MOT', 'SAMSUNG', 'SonyEricsson');
	var device_name = '';
	for (var word in mobileKeyWords){
		if (navigator.userAgent.match(mobileKeyWords[word]) != null){
			device_name = mobileKeyWords[word];
			break;
		}
	}
return device_name
}
< /script >


@ 활용 /========================================================

< script type="text/javascript" src="./js/function.js"  >< /script >
< script type="text/javascript" >
$(document).ready(function()
{
	// device check
	var device = check_device();
	if(device !=''){
		alert(device);
                // 모바일 페이지 이동 및 레이아웃 변경 작업 실행은 여기서 작성하심 됩니당
        }
});
< /script >


디바이스 정보를 알아 냈으니

원하는 작업을 하면 되겠죠?




2. PHP로 디바이스 정보 알아내기



@ 방법 1

< ?php
if (    strstr($_SERVER['HTTP_USER_AGENT'], "iPod") 
       || strstr($_SERVER['HTTP_USER_AGENT'], "iPhone")
       || strstr($_SERVER['HTTP_USER_AGENT'], "iPad")
       || strstr($_SERVER['HTTP_USER_AGENT'], "Mobile"))
 {
       // 모바일 페이지 이동 및 레이아웃 변경 작업 실행은 여기서 작성하심 됩니당
       header('Location: mobile.mysite.url');
       exit;
}
? >




@ 방법 2

< ?php
# 애플 제품인지 체크
function checkMobile() {
	if( preg_match( '/(iPod|iPhone|iPad|Android|Mobile)/', $_SERVER[ 'HTTP_USER_AGENT' ] ) ) {
		return 'true';
	} else {
		return 'false';
	}
}

if( checkMobile() ){
      // 모바일 페이지 이동 및 레이아웃 변경 작업 실행은 여기서 작성하심 됩니당
}
? >