IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

JavaScript笔记整理

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

整理一篇工作中的JavaScript脚本笔记,不定时更新,笔记来自网上资料或者自己经验归纳。

(1) 获取Url绝对路径

function getUrlRelativePath()
    {
        var url = document.location.toString();

        var arrUrl = url.split("//");

        var start = arrUrl[1].indexOf("/");
        var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符

        if(relUrl.indexOf("?") != -1){
          relUrl = relUrl.split("?")[0];
        }
        return relUrl;
    }

(2) 获取Url请求参数

function GetRequest() {
  var url = location.search; //获取url中"?"符后的字串
  var theRequest = new Object();
  if (url.indexOf("?") != -1) {
    var str = url.substr(1);
    strs = str.split("&");
    for(var i = 0; i < strs.length; i ++) {
      theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
  }
  return theRequest;
}
var Request = new Object();
Request = GetRequest();

(3) 获取特定请求参数

function getQueryString(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}
// 这样调用:
alert(GetQueryString("参数名1"));

(4)stringify函数

将现有的对象转换为JSON字符串, 则可以使用 JSON.stringify(obj)函数

(5) setTimeOut函数

javascript每隔3秒执行method函数
setTimeout(function(){ method()},3000);

(6) js 获取当前年月日时分秒星期

来自https://tech.souyunku.com/wdw31210/archive/2012/06/27/2565865.html

$("#aa").click(function () {
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var currentTime = "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
alert(currentTime);
});

(7) Ajax请求显示加载中提示

$.ajax({
    type: "post",
    url: loadurl,
    async: true,
    i:Math.random(),
    success:function(data){
        $("#tra_"+id).html(data);
    }
    beforeSend:function(){
        $("#tra_"+id).html('加载中...');
    }
});

(8) 大量if…else…的替换方法

遇到需要写很多if…else…的情况,或许可以考虑如下的哈希字典匹配的方法,或者用状态模式实现

例子:

/* 不用if...else,改成哈希字典匹配的方法 */
                        //if...else...方法
                        /* var itemTypeStr = '';
                        if(rowdata.itemType == '1'){
                            itemTypeStr = '行政许可';
                        }else if(rowdata.itemType == '2'){
                            itemTypeStr = '非行政许可';
                        }else if(rowdata.itemType == '3'){
                            itemTypeStr = '公共服务事项';
                        } */
                        var itemTypeReg = {
                                '0':'',
                                '1':'行政许可',
                                '2':'非行政许可',
                                '3':'公共服务事项',
                                '4':'备案',
                                '5':'其他',
                                '6':'行政征收',
                                '7':'行政确认',
                                '8':'行政年检',
                                '9':'其他行政权力',
                                '10':'行政处罚',
                                '11':'行政强制',
                                '12':'行政给付',
                                '13':'行政检查',
                                '14':'行政奖励',
                                '15':'行政裁决'
                        }
                        var itemType = rowdata.itemType;
                        //itemType为1~15的数,哈希匹配的方法,例子仅供参考
                        itemType = itemTypeReg[itemType];
                        return itemType;


(9) 字符串长度获取(支持中文)

获取字符串的长度,有时候经常就是str.length直接获取,其实在字符串没有中文的情况是可以的,但是一旦有中文,就会发现这样获取其实是不正确的。因为中文占两个字节


function getStrRealLen(str) { ///<summary>获得字符串实际长度,中文2,英文1</summary> ///<param name="str">要获得长度的字符串</param> var realLength = 0, len = str.length, charCode = -1; for (var i = 0; i < len; i++) { charCode = str.charCodeAt(i); if (charCode >= 0 && charCode <= 128) realLength += 1; else realLength += 2; } return realLength; };

(10) window.open打开之后关闭刷新实现

有些时候,要打开一个弹窗是用window.open实现,然后我尝试在弹窗页面做关闭窗口的上级页面刷新,发现并没有效果,网上找资料,找到一篇很好的博客 https://blog.csdn.net/wangshanny/article/details/46325537

/**
  * 监听打开的弹窗,关闭后刷新页面
  */
 function openWin(url,text,winInfo){
    var winObj = window.open(url,text,winInfo);
    var loop = setInterval(function() {     
        if(winObj.closed) {    
            clearInterval(loop);    
            //alert('closed');    
            parent.location.reload(); 
        }    
    }, 1);   
 }





文章永久链接:https://tech.souyunku.com/?p=24390


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(61) 打赏



未经允许不得转载:搜云库技术团队 » JavaScript笔记整理

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367