프로그램/script

바로 사용할 수 있는 javascript common 함수 13 가지

mulderu 2012. 1. 11. 10:20
 
쉽게 갈수 있다면 쉽게 가죠.
참고로 여기 있는 스크립트는 여기저기서 카피한것입니다... 출처가 여러군데여서 ... 그냥 모두에게 감사 드립니다.

DO
COPY & PASTE :)



// 플래쉬를 손쉽게 사용하기. /** @id Flash */ var Flash = function (sSwfUrl, iWidth, iHeight, sWmode) { this.params = {}; this.vals = {}; this.atts = {}; if (sSwfUrl) { this.setAtt("src", sSwfUrl); this.setParam("movie", sSwfUrl); } if (iWidth) { this.setAtt("width", iWidth); } if (iHeight) { this.setAtt("height", iHeight); } if (sWmode) { this.setParam("wmode", sWmode); } this.setAtt("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"); this.setAtt("type", "application/x-shockwave-flash");
this.setAtt("codebase", "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"); this.setParam("quality", "high"); this.setParam("allowScriptAccess", "always"); this.setParam("swliveconnect", "true"); }; Flash.prototype = { /** @id Flash.setAtt */ setAtt : function (sName, vValue) { this.atts[sName] = vValue; }, /** @id Flash.getAtt */ getAtt : function (sName) { return this.atts[sName]; }, /** @id Flash.setVal */ setVal : function (sName, vValue) { this.vals[sName] = vValue; }, /** @id Flash.getVal */ getVal : function (sName) { return this.vals[sName]; }, /** @id Flash.setValByArray */ setValByArray : function (aName, aValue) { if (aName !== null && aValue !== null && aName.length === aValue.length) { for (var i = 0, n = aName.length ; i < n ; i++){ this.setVal(aName[i], aValue[i]); } } }, /** @id Flash.setParam */ setParam : function (sName, vValue) { this.params[sName] = vValue; }, /** @id Flash.getParam */ getParam : function (sName) { return this.params[sName]; }, /** @id Flash.getHTML */ getHTML : function () { var a = []; var i; for (i in this.vals) { if (this.vals.hasOwnProperty(i)) { a.push(i + "=" + this.vals[i]); } } var v = a.join("&"); if (v!==null && v.length > 0) {
this.setParam("flashvars", v);
}
var s = "";
var k;
if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
s = "<embed ";
for (k in this.atts) {
if (this.atts.hasOwnProperty(k)) {
s += k;
s += "=\"";
s += this.atts[k];
s += "\" ";
}
}
for (k in this.params) {
if (this.params.hasOwnProperty(k)) {
s += k;
s += "=\"";
s += this.params[k];
s += "\" ";
}
}
s += "/>";
} else {
s = "<object ";
for (k in this.atts) {
if (this.atts.hasOwnProperty(k)) {
s += k;
s += "=\"";
s += this.atts[k];
s += "\" ";
}
}
s += ">\n";
for (k in this.params) {
if (this.params.hasOwnProperty(k)) {
s += "<param name=\"";
s += k;
s += "\" value=\"";
s += this.params[k];
s += "\" />";
}
}
s += "</object>";
}
return s;
}
};

  

// 팝업오픈하기...
/** @id openWindow */
function openWindow(sURL, sWindowName, iWidth, iHeight, sScroll) {
var x = (screen.width - iWidth) / 2;
var y = (screen.height - iHeight) / 2;
if (sScroll === null) {
sScroll = "no";
}
var s = "";
s += "toolbar=no, channelmode=no, location=no, directories=no, resizable=no, menubar=no";
s += ", scrollbars=";
s += ", left=";
s += x;
s += ", top=";
s += y;
s += ", width=";
s += iWidth;
s += ", height=";
s += iHeight;
var win = window.open(sURL, sWindowName, s);
return win;
}



// 쿠키관리를 좀더 나이스하게 하기..
/** @id Cookie */
var Cookie = {
/** @id Cookie.set */
set : function (sCookieName, sCookieValue, iExpireDays, sDomain) {
if (!iExpireDays) {
iExpireDays = 1;
}
if (!sDomain) {
sDomain = "ad.naver.com";
}
var d = new Date();
d.setHours(24*iExpireDays);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
var c = escape(sCookieName);
c += "=";
c += escape(sCookieValue);
c += "; path=/; expires=";
c += d.toGMTString();
c += "; domain=";
c += sDomain;
document.cookie = c;
},
/** @id Cookie.get */
get : function (sCookieName) {
var aC = document.cookie.match(new RegExp("(^|;)\\s*" + escape(sCookieName) + "=([^;\\s]+)"));
return (aC ? unescape(aC[2]) : null);
},
/** @id Cookie.exist */
exist : function (sCookieName) {
var v = Cookie.get(sCookieName);
if (!v) {
return false;
}
return (v.toString() !== "");
}
};

 

// 주어진 class 를 가지고 있는 모든 element를 리턴
function getElementsByClass( searchClass ) { var classElements = []; var els = document.getElementsByTagName('*'); var elsLen = els.length for (i = 0, j = 0; i < elsLen; i++) { var classes = els[i].className.split(' '); for (k = 0; k < classes.length; k++) if ( classes[k] == searchClass ) classElements[j++] = els[i]; } return classElements; }

// 리턴 [ x, y, width, height ]
function getElementProperties( element ) { var x = 0; var y = 0; var width = element.offsetWidth; var height = element.offsetHeight; do { x += element.offsetLeft; y += element.offsetTop; } while ( element = element.offsetParent ); return [ x, y, width, height ]; }

// ----------------------------------------------------------------------------

//
//  jQuery 의 $(document).ready(); 와 같이 브라우저 로딩시 시작될 자동 함수 추가
//
function AddOnload(myfunc) {
  if (window.addEventListener)
    window.addEventListener('load', myfunc, false);
  else if (window.attachEvent)
    window.attachEvent('onload', myfunc);
}

// ----------------------------------------------------------------------------

// Cookie굽기
function setCookie(c_name, value, expiredays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie = c_name + "=" + escape(value) +
      (expiredays > 0 ? ";expires=" + exdate.toGMTString() : "");
}

// ----------------------------------------------------------------------------

//  Cookie 가져오기
function getCookie(c_name) {
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1;
      c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) c_end = document.cookie.length;
      return unescape(document.cookie.substring(c_start, c_end));
    }
  }
  return "";
}

// ----------------------------------------------------------------------------

// query 값중 해당 key 의 값을 구함
function getQueryStringValue(url, key) {
  var val = ''
   var i0 = url.indexOf('&' + key + '=')
  if (i0 < 0) {
    i0 = url.indexOf('?' + key + '=')
  }
  if (i0 > -1) {
    i0 += key.length + 2;
    var i1 = url.indexOf('&', i0);
    if (i1 < 0) {
      i1 = url.length;
    }
    val = url.substring(i0, i1);
  }
  return val;
}

// ----------------------------------------------------------------------------

// 간단히 팝업을 만들어 올린다.
function popIt(width, height, scrollbars, pname, url) {
  if (!width) width = "500";
  if (!height) height = "500";
  if (!scrollbars) scrollbars = 1;
  var windowString = 'width=' + width + ',height=' + height + ',resizable=0,status=0,toolbar=no,scrollbars=' + scrollbars + ',scrolling=auto';
  var popItWindow = window.open(url, pname, windowString);
  return false;
}

// ----------------------------------------------------------------------------

// 단순히 팝업 올리기 
function popUpSize(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=558,height=575,left = 490,top = 362');");
}

// ----------------------------------------------------------------------------

// async하게 javascript를 로드 한다.
function LoadScript(src, charset) {
  var aainvscript = document.createElement("script");
  aainvscript.type = "text/javascript";
  aainvscript.src = src;
  aainvscript.charset = (charset ? charset : 'ISO-8859-1');
  document.getElementsByTagName('head')[0].appendChild(aainvscript);
}

// ----------------------------------------------------------------------------

// 해당 레이어의  Opacity (투명도)를 지정한다.
function SetOpacity(object, oValue) {
  if (object) {
    object.style.filter = 'alpha(opacity=' + oValue + ')';
    object.style.MozOpacity = oValue / 100;
    object.style.opacity = oValue / 100;
  }
}

// ----------------------------------------------------------------------------

function BlendOpacity(id, msDuration, msStart, t0, t1) {
    var element = document.getElementById(id);
    var opacity = element.style.opacity * 100;
    var msNow = (new Date()).getTime();
    opacity = t0 + (t1 - t0) * (msNow - msStart) / msDuration;
    if (opacity < 0)
        SetOpacity(element, 0)
    else if (opacity > 100)
        SetOpacity(element, 100)
    else {
        SetOpacity(element, opacity);
        element.timer = window.setTimeout("BlendOpacity('" + id + "'," + msDuration + "," + msStart + "," + t0 + "," + t1 + ")", 1);
    }
}

// ----------------------------------------------------------------------------

function ImageTransition(styleID, newImage) {
  var foregroundID = 'im_' + styleID
  if (document.all) {
    var thumb = document.getElementById(foregroundID);
    thumb.style.filter = "blendTrans(duration=0.2)";
    thumb.filters.blendTrans.apply();
    thumb.filters.blendTrans.play();
    thumb.src = newImage
  }
  else {
    var backgroundID = 'dv_' + styleID;
    var foreground = document.getElementById(foregroundID);
    if (backgroundID) {
      var background = document.getElementById(backgroundID);
      if (background) {
        background.style.backgroundImage = 'url(' + foreground.src + ')';
        background.style.backgroundRepeat = 'no-repeat';
      }
    }
    if (foreground) {
      SetOpacity(foreground, 0);
      foreground.src = newImage;
      if (foreground.timer) window.clearTimeout(foreground.timer);
      var startMS = (new Date()).getTime();
      foreground.timer = window.setTimeout("BlendOpacity('" + foregroundID + "',200," + startMS + ",0,100)", 10);
    }
  }
}


 // ----------------------------------------------------------------------------

function showRegion(tab) {
  cc = document.getElementById('div' + tab);
  if (cc.style.display == "none") {
    $('#img' + tab).removeClass('arrow-close');
    $('#img' + tab).addClass('arrow-open');
    cc.style.display = "";
  }
  else {
    $('#img' + tab).removeClass('arrow-open');
    $('#img' + tab).addClass('arrow-close');
    cc.style.display = "none";
  }
}

// ----------------------------------------------------------------------------

var hideRegionsHandler;
function _HideRegions() {
  var tab = "Countries";
  cc = document.getElementById('div' + tab); 
  $('#img' + tab).removeClass('arrow-open');
  $('#img' + tab).addClass('arrow-close');
  cc.style.display = "none";
  hideRegionsHandler = null;
}


log = function() { var message = Array.prototype.join.call(arguments, " "); }
// ----------------------------------------------------------------------------

function HideRegions(hide) {
  if (hide) {
      hideRegionsHandler = setTimeout('_HideRegions()', 500);
  }
  else {
    if (hideRegionsHandler) {
      clearTimeout(hideRegionsHandler)
    }
  }
}