• javascript代码库


    代码
    //正则表达试


    function RegCheck(reg,str)//reg--数组,str字符串
    {
    var sign=false;
    for (var i=0;i<reg.length;i++)
    {
    if(reg[i].test(str)==true)
    {
    sign
    =true
    break;
    }
    }
    return sign;
    }
    function isNUmber(str)//验证字符串是否是数字
    {
    var reg = new Array(/^-?\d+$/,/^(-?\d+)(\.\d+)?$/);
    return RegCheck(reg,str);
    }


    //×××××××××正则表达式使用**************//

    //验证是否是数字

    function regIsNumber(fData)
    {
    var reg = new RegExp("^[-]?[0-9]+[\.]?[0-9]+$");
    return reg.test(fData)
    }

    //×××××××××正则表达式使用**************//



    //javascript操作cookie

    function addCookie(objName,objValue,objHours){//添加cookie
    var str = objName + "=" + escape(objValue);
    if(objHours &gt; 0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
    var date = new Date();
    var ms = objHours*3600*1000;
    date.setTime(date.getTime()
    + ms);
    str
    += "; expires=" + date.toGMTString();
    }
    document.cookie
    = str;
    alert(
    "添加cookie成功");
    }

    function getCookie(objName){//获取指定名称的cookie的值
    var arrStr = document.cookie.split("; ");
    for(var i = 0;i < arrStr.length;i ++){
    var temp = arrStr[i].split("=");
    if(temp[0] == objName) return unescape(temp[1]);
    }
    }

    function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
    var date = new Date();
    date.setTime(date.getTime()
    - 10000);
    document.cookie
    = name + "=a; expires=" + date.toGMTString();
    }
    //读取出来所有的cookie字筗串了
    function allCookie(){//读取所有保存的cookie字符串
    var str = document.cookie;
    if(str == ""){
    str
    = "没有保存任何cookie";
    }
    alert(str);
    }


    //flash调用代码--ur图片地址,w--width,h-height

    function flash(ur,w,h){
    document.write(
    '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="obj1" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="'+w+'" height="'+h+'">');
    document.write(
    '<param name="movie" value="'+ur+'">');
    document.write(
    '<param name="quality" value="high"> ');
    document.write(
    '<param name="wmode" value="transparent"> ');
    document.write(
    '<param name="menu" value="false"> ');
    document.write(
    '<embed src="'+ur+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="obj1" width="'+w+'" height="'+h+'" quality="High" wmode="transparent">');
    document.write(
    '</embed>');
    document.write(
    '</object>');
    }

    //alert自动关闭

    function autoCloseAlert(msg)
    {
    var tempFrm=document.getElementById("msgFrm");
    if(tempFrm==null||typeof(tempFrm)=="undefined")
    {
    tempFrm
    =document.createElement("iframe");
    document.body.appendChild(tempFrm);
    tempFrm.id
    = "msgFrm";
    tempFrm.name
    ="msgFrm";
    tempFrm.style.display
    ="none";
    }
    msg
    =msg.replace(/"/g,"\\\"");
    msg=msg.replace(/\n/g,
    "\\n");
    var sfeature=
    "status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;";
    msgFrm.showModelessDialog(
    "javascript:alert(\""+msg+"\");window.close();","",sfeature);
    setTimeout(
    "msgFrm.location.reload();",1000);
    }
    //重写弹出框
    var oldAlert = window.alert;
    window.alert = function(msg)
    {
    var tempEvent=window.event;
    if(tempEvent==null)
    {
    oldAlert(msg);
    }else
    {
    autoCloseAlert(msg);
    }
    }

    //按相对比例显示图片大小

    /*按比例生成缩略图*/
    function DrawImage(ImgD,W,H){
    var flag=false;
    var image=new Image();
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= W/H){
    if(image.width>W){
    ImgD.width=W;
    ImgD.height=(image.height*H)/image.width;
    }
    else{
    ImgD.width=image.width;
    ImgD.height=image.height;
    }
    ImgD.alt=
    "";
    }
    else{
    if(image.height>H){
    ImgD.height=H;
    ImgD.width=(image.width*W)/image.height;
    }
    else{
    ImgD.width=image.width;
    ImgD.height=image.height;
    }
    ImgD.alt=
    "";
    }
    }
    }

    //批量替换标签内容

    function ReplaceByTagName(tagName,findContent,replaceContent)
    {
    var divs=document.getElementsByTagName(tagName);
    for (var i=0;i<divs.length;i++ )
    {
    if (divs[i].innerHTML.search(findContent)>-1)
    {
    divs[i].innerHTML=replaceContent;
    }

    }

    }

    //Form表单操作

    function getRadioCheckedValue(str)//获得被选单选按钮的值
    {
    var d = document.getElementsByName(str);
    for(var i = 0; i < d.length; i++)
    {
    if(d[i].checked){
    return d[i].value;
    }
    }
    }


    //判断浏览器版本

    function getOs()
    {
    if(navigator.userAgent.indexOf(
    "MSIE")>0)return 1;
    if(isFirefox=navigator.userAgent.indexOf(
    "Firefox")>0)return 2;
    if(isSafari=navigator.userAgent.indexOf(
    "Safari")>0)return 3;
    if(isCamino=navigator.userAgent.indexOf(
    "Camino")>0)return 4;
    if(isMozilla=navigator.userAgent.indexOf(
    "Gecko/")>0)return 5;
    return 0;
    }

    //判断是否为数字---false:不是数字

    function isNum(str) {
    //判断是否是整数
    //
    str = parseInt(str);
    //
    alert(str);
    var i;
    for(i=0; i<str.length; i++) {
    if (str.charAt(i) == ".") {
    return false;
    }
    }
    return !isNaN(str);

    //return (!isNaN(parseInt(str))) ? true : false;


    }

    function checkNum(str)
    {
    if (isNaN(str))
    {
    return false;
    }

    else

    {

    returen
    true;

    }

    }

    //将字符串转换为日期

    function Swith(strdate){
    var strYear=strdate.substring(0,4);
    var strMonth=strdate.substring(5,7);
    var strDay=strdate.substring(8,10);
    var strHours=strdate.substring(11,13);
    var strMinutes=strdate.substring(14,17);
    return new Date(strYear,strMonth,strDay,strHours,strMinutes);
    }

    //获得两个日期相减的值 -dateM:相减值;datetype:取值类型


    function GetTime(dateM,datetype){
    var s; // 声明变量。
    var MinMilli = 1000 * 60; // 初始化变量。
    var HrMilli = MinMilli * 60;
    var DyMilli = HrMilli * 24;
    s
    ="";
    if (datetype=="d"){
    s
    +=Math.round(Math.abs(dateM/DyMilli));
    }else if (datetype=="h"){
    s
    +=Math.round(Math.abs(dateM/HrMilli))+"小时"
    }else if (datetype=="m"){
    s
    +=Math.round(Math.abs(dateM/MinMilli))+"分";
    } else{
    s
    +=Math.round(Math.abs(dateM/1000))+"秒"
    }
    return(s); // 返回结果。
    }

    //js判断是否为日期

    function isDateString(sDate)
    {
    var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
    var iaDate = new Array(3)
    var year, month, day

    if (arguments.length != 1) return false
    iaDate
    = sDate.toString().split("-")
    if (iaDate.length != 3) return false
    if (iaDate[1].length > 2 || iaDate[2].length > 2) return false

    year
    = parseFloat(iaDate[0])
    month
    = parseFloat(iaDate[1])
    day
    =parseFloat(iaDate[2])

    if (year < 1900 || year > 2100) return false
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
    if (month < 1 || month > 12) return false
    if (day < 1 || day > iaMonthDays[month - 1]) return false
    return true
    }

    //iframe高度自适应

    function reinitIframe(iframeID){

    var iframe = document.getElementById(iframeID);

    try{

    var bHeight = iframe.contentWindow.document.body.scrollHeight;

    var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;

    var height = Math.max(bHeight, dHeight);

    iframe.height
    = height;

    }
    catch (ex){}

    }

    //创建xmldom
    function getXmldom()
    {
    if (window.ActiveXObject){
    var xmlDom=new ActiveXObject("Microsoft.XMLDOM");
    }
    else{
    if (document.implementation&&document.implementation.createDocument){
    var xmlDom=document.implementation.createDocument("","doc",null);
    }
    }
    xmlDom.async
    = false;
    xmlDom.preserveWhiteSpace
    =true;
    return xmlDom;
    }
    //加载xml文档
    function loadXML(xmlFile)

    {

    var xmlDoc;

    if(window.ActiveXObject)

    {

    xmlDoc
    = new ActiveXObject('Microsoft.XMLDOM');

    xmlDoc.async
    = false;

    xmlDoc.load(xmlFile);

    }

    else if (document.implementation&&document.implementation.createDocument)

    {

    xmlDoc
    = document.implementation.createDocument('', '', null);

    xmlDoc.load(xmlFile);

    }

    else

    {

    return null;

    }

    return xmlDoc;

    }

    //select跳转地址
    function GoUrl(id)
    {
    var obj;
    obj
    =document.getElementById(id);
    if (obj.value!=0)
    {
    window.open(obj.value);
    }
    }
    // 说明:用 javascript 实现网页图片等比例缩放
    function drawimage(imgd,fitwidth,fitheight)
    {
    var image=new Image();
    image.src
    =imgd.src;
    //image.src=document.getElementById(imgd).src;
    if(image.width>0 && image.height>0)
    {
    if(image.width/image.height>= fitwidth/fitheight)
    {
    if(image.width>fitwidth)
    {
    imgd.width
    =fitwidth;
    imgd.height
    =(image.height*fitwidth)/image.width;
    }
    else
    {
    imgd.width
    =image.width;
    imgd.height
    =image.height;
    }
    }
    else
    {
    if(image.height>fitheight)
    {
    imgd.height
    =fitheight;
    imgd.width
    =(image.width*fitheight)/image.height;
    }
    else
    {
    imgd.width
    =image.width;
    imgd.height
    =image.height;
    }
    }
    }
    }
    //网页将不能被另存为
    function NoDown()
    {
    document.write(
    "<noscript><iframe src=*.html></iframe></noscript>");
    }
    //状态栏的特效显示
    function cm(str){
    self.status
    =str;
    setTimeout(
    "cm()",0);
    }

    function SetCookie(name,value,expire) {
    var exp = new Date();
    exp.setTime(exp.getTime()
    + expire);
    document.cookie
    = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }

    function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;
    }

    function delCookie(name){
    var exp = new Date();
    exp.setTime(exp.getTime()
    - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
    }
    //等比例缩放图片
    function drawimage(imgd,fitwidth,fitheight)
    {
    var image=new Image();
    image.src
    =imgd.src;
    //image.src=document.getElementById(imgd).src;
    if(image.width>0 && image.height>0)
    {
    if(image.width/image.height>= fitwidth/fitheight)
    {
    if(image.width>fitwidth)
    {
    imgd.width
    =fitwidth;
    imgd.height
    =(image.height*fitwidth)/image.width;
    }
    else
    {
    imgd.width
    =image.width;
    imgd.height
    =image.height;
    }
    }
    else
    {
    if(image.height>fitheight)
    {
    imgd.height
    =fitheight;
    imgd.width
    =(image.width*fitheight)/image.height;
    }
    else
    {
    imgd.width
    =image.width;
    imgd.height
    =image.height;
    }
    }
    }
    }
    //-------------------自动缩放图片大小--指定web区域
    function updateShape(domID,width,height)
    {
    var obj;
    obj
    =document.getElementById(domID);
    var count;
    count
    =obj.getElementsByTagName("img").length;
    if(count>0)
    {
    for (var i=0;i<count;i++)
    {
    drawimage(obj.getElementsByTagName(
    "img")[i] ,width,height);
    }
    }
    }
    function updateShapeByDomID(domID,width,height)
    {
    var do1=setInterval("updateShape(\""+domID+"\","+width+","+height+")",1);
    function stop1()
    {
    window.clearInterval(do1);
    }
    setTimeout(
    "stop1()",8000);
    }
    //-------------------自动缩放图片大小--指定web区域
    //
    使png图片在ie6下透明显示
    //
    调用方法
    /*

    if(navigator.userAgent.indexOf("MSIE")>-1)
    {
    window.attachEvent("onload", correctPNG);
    };

    */
    function correctPNG()
    {
    for(var i=0; i<document.images.length; i++)
    {
    var img = document.images[i]
    var imgName = img.src.toUpperCase()
    if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
    {
    var imgID = (img.id) ? "id='" + img.id + "' " : ""
    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
    var imgStyle = "display:inline-block;" + img.style.cssText
    if (img.align == "left") imgStyle = "float:left;" + imgStyle
    if (img.align == "right") imgStyle = "float:right;" + imgStyle
    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
    var strNewHTML = "<span " + imgID + imgClass + imgTitle
    + " style=\"" + "" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
    +
    "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
    +
    "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
    img.outerHTML
    = strNewHTML
    i
    = i-1
    };
    };
    };

    if(navigator.userAgent.indexOf("MSIE")>-1)
    {
    window.attachEvent(
    "onload", correctPNG);
    };
    为了实现梦想,无论遇到多么大的困难,都不退缩!
  • 相关阅读:
    关于lucene断点续索引和增量索引的问题
    发布一个关于统计时间段的MDX语句
    【蛙蛙推荐】想设计一个关于软件开发的元搜索引擎,希望大家支持
    python中and和or的用法
    Hadoop实战中高级部分 之 Hadoop MapReduce高级编程
    (转)对实时分析与离线分析的思考(二)
    数据分析站点导航
    MapReduce:详解Shuffle过程
    分析能力的8个等级(My Level)
    (转)Tire Tree
  • 原文地址:https://www.cnblogs.com/lv_yantao/p/1835271.html
Copyright © 2020-2023  润新知