javascriptユーティリティ

18044 ワード

function addBookMark(url, title){
//        addBookMark(window.location,document.title) 
    if(document.all){
      window.external.addFavorite(url,title);
    }else if (window.sidebar){
      window.sidebar.addPanel(title,url,'');
    }else{
      alert('    ');
    }
}   


function setHomepage(obj,url) {
// setHome(this, window.location) 
    try {   
        obj.style.behavior = 'url(#default#homepage)';   
        obj.setHomePage(url);   
    } catch (e) {   
        if (window.netscape) {   
            try {   
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
            } catch (e) {   
                alert('         !
"about:config"
[signed.applets.codebase_principal_support] "true", 。'); } var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch); prefs.setCharPref('browser.startup.homepage', vrl); } } } function boxMove(box){ // var w = box.scrollWidth,h = box.scrollHeight; var iWidth = document.documentElement.clientWidth; var iHeight = document.documentElement.clientHeight; var moveX = 0,moveY = 0,moveTop = 0,moveLeft = 0,moveable = false; box.onmousedown = function(e){ moveable = true; e = window.event?window.event:e; moveX = e.clientX-box.offsetLeft; moveY = e.clientY-box.offsetTop; box.style.zIndex++; } document.onmousemove = function(e){ if(moveable){ e = window.event?window.event:e; var x = e.clientX - moveX; var y = e.clientY - moveY; if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight)){ box.style.left = x + "px"; box.style.top = y + "px"; box.style.margin = "auto"; } } } document.onmouseup = function (){moveable = false;}; } function getHtmlRoot(){ /* html */ if(document.compatMode.toLowerCase()=="css1compat"){ htmlRoot = document.documentElement; }else{ htmlRoot = document.body; } return htmlRoot; } function showWindow(boxId, closeId, showBg){// htmlRoot = getHtmlRoot(); box = document.getElementById(boxId); boxId = '#' + boxId; closeId = '#' + closeId; showBox = $(boxId); showBox.show(); z_index = 20; moveLeft = (htmlRoot.clientWidth- box.clientWidth)/2 + htmlRoot.scrollLeft +'px'; moveTop = (htmlRoot.clientHeight - box.clientHeight)/2 + htmlRoot.scrollTop;+'px'; showBox.css({position:'absolute', left:moveLeft,zIndex:z_index, top:moveTop}); boxMove(box); if(showBg){ objMask = document.createElement("div"); objMask.className = "BoxMask"; htmlRoot.appendChild(objMask); objMask.style.cssText += 'position:absolute;top:0; left:0;filter:Alpha(Opacity=50);opacity:0.5;background:#AAA;'; objMask.style.zIndex = z_index -1; objMask.style.width = htmlRoot.clientWidth + 'px'; objMask.style.height = htmlRoot.scrollHeight + htmlRoot.scrollTop + 'px'; } $(closeId).click(function(){ showBox.hide(); mybg.style.display = "none"; }); $('.closeBtn').click = function(){ showBox.hide(); mybg.style.display = "none"; }; } function getFormQuery(formId){ /* */ formObj = document.getElementById(formId); var i, queryString = "", and = "", itemValue; for(i = 0; i<formObj.length; i++ ){ var item = formObj[i]; if ( item.name!='' ){ if(item.type == 'select-one'){ itemValue = item.options[item.selectedIndex].value; }else if ( item.type=='checkbox' || item.type=='radio'){ if ( item.checked == false ){ continue; } itemValue = item.value; }else if ( item.type == 'button' || item.type == 'submit' || item.type == 'reset' || item.type == 'image'){ continue; }else{ itemValue = item.value; } // itemValue = escape(itemValue); queryString += and + item.name + '=' + itemValue; and="&"; //queryString += and + encodeURIComponent(item.name) + '=' +encodeURIComponent( itemValue); } } return queryString; } // js onerror = errHandle; function errHandle(msg,url,line){ var txt="" txt = " !

" txt += " : "+ msg +"
" txt += " : " + url + "
" txt += " : " + line + "

" alert(txt); return false; } function setAutoWidth(id,width,size){ // var obj = document.getElementById(id); if(size=='max'){ obj.style.width = (obj.clientWidth > width) ? width + "px" : "auto"; }else{ obj.style.width = (obj.clientWidth < width) ? width + "px" : "auto"; } } function fontScroll(id){ /* */ var obj = document.getElementById(id); var text = obj.innerHTML; var first = text.charAt(0); var left = text.substring(1, text.length); obj.inerHTML = left + first; // setInterval('fontScroll(id)', 500); } function bubbleSort(arr){ /* */ var sign = false // for(var i=0; i < arr.length-1; i++){ for(varj=0; j < arr.length-1-i; j++){ if(arr[j]> arr[j+1]){ vartemp = arr[j] arr[j]= arr[j+1] arr[j+1]= temp sign= true // , , } } if(sign)// sign= false // else break// } return arr; } function getCoordinate(evt){ /* */ var x = evt.clientX; var y = evt.clientY; document.getElementById('show').innerHTML = x +' &' + y; } function checkEmail(email){ /* (;) Email */ if(email != null){ if(email.indexOf(";",0) == -1){ //indexOf( , ) , -1 if(!isEmail(email)){ alert(" , !"); document.getElementById("email").focus(); return false; } }else{ var emailArr = email.split(";");//split( , ) var i, iMax = emailArr.length; for(i = 0; i < iMax; i++){ if(emailArr[i] != null || emailArr != ""){ if(!isEmail(emailArr[i])){ alert(" , !"); document.getElementById("email").focus(); return false; } } } } } function isEmail(str){ var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/; return reg.test(str);//test( ) . } }