個人常用js方法分享

2021-09-11 21:55:28 字數 1512 閱讀 4323

最近寫了很多h5專案,發現h5開發很多東西都是相同的,所以本人就將其中公用的方法羅列封裝了,有需要用到的小夥伴可以借鑑,當然,如果有不夠詳盡或者有錯誤的地方,也希望大佬們多多指正。

首先,定義全域性變數,老生常談的封裝問題。

(function(w);

w.tsmethod = tsmethod;

})(window)

我要做的就是將所有的公共方法全部拓展到我定義的這個tsmethod物件上,當然了,我這裡總結的方法是本人在實踐專案內總結的,可能涵蓋面還比較侷限,之後會不斷更新的,還希望大佬們多多給意見,多多指導。

1、根據環境獲取後台訪問網域名稱(區分是線上環境還是測試環境)

tsmethod.gethost = function()

if(window.location.href.indexof("www.???")>-1)

}

}

3、獲取主頁鏈結(用於分享或者複製鏈結等)

tsmethod.webhost = function()

if(window.location.href.indexof("線上環境位址")>-1)

}

4、cookie方法的處理(為了防止與瀏覽器cookies衝突(重名等))

tsmethod.setcookie =function(c_name, value, expiredays)

tsmethod.getcookie = function(name)

tsmethod.delcookie = function(name)

5、第三方路由引數值的獲取(code和accesstoken)

tsmethod.parsequerystring = function (url) ;

if (arr_url && arr_url[1])

}return ret;

}

6、裝置檢測(pc端/移動端(安卓/ios))

tsmethod.ispc = function() 

}return flag;

}tsmethod.isios = function()

7、時間格式處理(後端返回的時間格式可能與設定格式往往是有出入的,需要前端自己處理)處理變數私有化的核問題

tsmethod.changetime = function(time,b)

var day = data.getdate();

if(day<10)

var hour=data.gethours();

if(hour<10)

var minute=data.getminutes();

if(minute<10)

if(b==1)

if(b==2)

if(b==3)

return time;

}

js工具 js常用方法,js常用判斷方法

function obj id 根據id得到物件 function val id 根據id得到物件的值 function trim str 刪除左邊和右邊空格 function ltrim str 刪除左邊空格 function rtrim str 刪除右邊空格 function isempty s...

js 常用方法

驗證數字,保留一位小數 function one ele a za z u4e00 u9fa5 驗證固定 和手機 function checkphone e 2 9 0 9 0 9 if e.value.match re 1 3 4 5 8 0 9 d test e.value else 驗證身份證...

js 常用方法

c c.substring 1,c.length 有的話,從第二位開始取 if c.indexof nameeq 0 return false 清除cookie function clearcookie name 設定cookie function setcookie name,value,seco...