JavaScript工作指令碼筆記整理
整理一篇工作中的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 獲取當前年月日時分秒星期
$("#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; }; 前端進階者學習交流q-u-n---731771211
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69901074/viewspace-2564031/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 近幾日工作筆記整理筆記
- jquery 工作筆記,不斷整理中..jQuery筆記
- 【轉】javascript日期操作詳解(指令碼之家整理)JavaScript指令碼
- Shell指令碼學習筆記指令碼筆記
- shell指令碼程式設計筆記指令碼程式設計筆記
- python指令碼練習筆記Python指令碼筆記
- linux shell 指令碼攻略筆記Linux指令碼筆記
- shell指令碼學習筆記-1指令碼筆記
- [筆記]OTN上的sql指令碼筆記SQL指令碼
- [筆記]一些SQL指令碼筆記SQL指令碼
- ActiveMQ筆記整理MQ筆記
- 隨記筆記未整理筆記
- 日常實用指令碼整理指令碼
- 工作筆記筆記
- mysql引擎筆記整理MySql筆記
- 筆記資源整理筆記
- 官方遊戲--學習筆記二(英雄指令碼)遊戲筆記指令碼
- 《Shell指令碼學習指南》學習筆記指令碼筆記
- Mysql 工作筆記MySql筆記
- 演算法筆記整理演算法筆記
- AutoreleasePool、Block、Runloop整理筆記BloCOOP筆記
- 過往業務筆記整理筆記
- Flutter筆記整理[待拆分]Flutter筆記
- JavaScript DOM程式設計藝術筆記2.1準備工作JavaScript程式設計筆記
- 用指令碼完成mysql工作指令碼MySql
- IBM X3650微碼重新整理筆記IBM筆記
- Linux 筆記分享十六:指令碼安裝包Linux筆記指令碼
- 按鍵精靈指令碼筆記:基本語法指令碼筆記
- 【連結】LINUX SHELL指令碼攻略筆記[速查]Linux指令碼筆記
- Shell指令碼常用命令整理指令碼
- 部分Windows批處理指令碼整理Windows指令碼
- JavaScript And Ajax(呈現指令碼塊、指令碼注入攻擊)JavaScript指令碼
- LDA臨時筆記,待整理LDA筆記
- 【pandas學習筆記】綜合整理筆記
- Redis學習整理筆記02Redis筆記
- SpringBoot整理筆記06Spring Boot筆記
- SpringBoot整理筆記08Spring Boot筆記
- 學習筆記——字串方法整理筆記字串