常用js脚本

来源:互联网 发布:js设置全局变量 编辑:程序博客网 时间:2024/05/16 00:35
/*随机数字(0~limit之间的随机数包含0和不包含limit)*/
function randomNumber(limit){
  
return Math.floor(Math.random()*limit);
}
/*冒泡排序*/
function bubbleSort(theArray)
{
  
var i, j;
  
for (i = theArray.length - 1; i >= 0; i--)
  {
    
for (j = 0; j <= i; j++)
    {
      
if (theArray[j+1< theArray[j])
      {
        
var temp = theArray[j];
        theArray[j] 
= theArray[j+1];
        theArray[j
+1= temp;
      }
    }
  }
  
return theArray;
}
/*字符替换*/
function replaceCharacters(conversionString,inChar,outChar)
{
  
var convertedString = conversionString.split(inChar);
  convertedString 
= convertedString.join(outChar);
  
return convertedString;
}
/*信息窗口*/
var msgWindow = null;
function messageWindow(title, msg)
{
  
var width="300", height="125";
  
var left = (screen.width/2- width/2;
  
var top = (screen.height/2- height/2;
  
var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
  
if(msgWindow==null){
      msgWindow 
= window.open("","msgWindow", styleStr);
      
var head = '<head><title>'+title+'</title></head>';
      
var body = '<center>'+msg+'<br><p><form><input type="button" value="   Done   " onClick="self.close()"></form>';
      msgWindow.document.write(head 
+ body);
  }
}
/*弹出窗口*/
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  
if(popUpWin)
  {
    
if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin 
= open(URLStr, 'popUpWin''toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
/*确认*/
confirm(messageStr);