新聞中心
URL截取參數(shù)

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、虹口網(wǎng)絡(luò)推廣、微信平臺(tái)小程序開(kāi)發(fā)、虹口網(wǎng)絡(luò)營(yíng)銷、虹口企業(yè)策劃、虹口品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供虹口建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
- //直接調(diào)用輸入想要截取的參數(shù)名稱幾個(gè)
- export function getParamFromUrl(key) {
- if (key === undefined) return null;
- let search = location.search.substr(1);
- let mReg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)');
- let mValue = search.match(mReg);
- if (mValue != null) return unescape(mValue[2]);
- return null;
- }
- //示例
- let city = getParamFromUrl('city');
JSON是否為空判斷
- //輸入想要檢測(cè)的json數(shù)據(jù) 如果為空返回返回false
- export function isNullObject(model) {
- if (typeof model === "object") {
- let hasProp = false;
- for (const prop in model) {
- hasProp = true;
- break;
- }
- if (hasProp) {
- return false;
- }
- return true;
- } else {
- throw "model is not object";
- }
- }
數(shù)據(jù)類型檢測(cè)
- //檢測(cè)變量的數(shù)據(jù)類型
- export function getParamType(item) {
- if (item === null) return null;
- if (item === undefined) return undefined;
- return Object.prototype.toString.call(item).slice(8, -1);
- }
- //返回String Function Boolean Object Number
獲取cookie
- //獲取document下cookie的具體某個(gè)參數(shù)值
- export function getCookie(key) {
- if (key === undefined) {
- return undefined;
- }
- let cookies = document.cookie;
- let mReg = new RegExp('(^|;)\\s*' + key + '=([^;]*)(;|$)');
- let mValue = cookies.match(mReg);
- let ret = undefined;
- if (mValue != null) {
- ret = unescape(mValue[2]);
- }
- if (ret !== undefined) {
- ret = ret.replace(/^\"|\'/i, '').replace(/\"|\'$/i, '');
- }
- return ret;
- }
版本號(hào)對(duì)比
一般在做APP端開(kāi)發(fā)的時(shí)候需要用到一些版本控制,那么就需要針對(duì)版本號(hào)來(lái)進(jìn)行對(duì)比,高版本或者低版本做一些特殊的邏輯處理,下面就是提供版本對(duì)比的方法
- //傳入要對(duì)比的版本號(hào),一般前面一個(gè)傳入當(dāng)前的版本號(hào),后面一個(gè)寫(xiě)上要對(duì)比的版本號(hào)
- export function versionCompare(higher, lower) {
- let sep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.';
- let higherAry = higher.split(sep),
- lowerAry = lower.split(sep);
- let l = Math.max(higherAry.length, lowerAry.length);
- for (let i = 0; i < l; i++) {
- let high = parseInt(higherAry[i] || 0);
- let low = parseInt(lowerAry[i] || 0);
- if (high > low) {
- return 1;
- }
- if (high < low) {
- return -1;
- }
- }
- return 0;
- }
- //返回值 higher > lower: 1;higher = lower: 0;higher < lower:-1
數(shù)組去重
- export function arrayUniq(array){
- let temp = [];
- for(var i = 0; i < array.length; i++){
- if(temp.indexOf(array[i]) == -1){
- temp.push(array[i]);
- }
- }
- return temp;
- }
iPhone X系列機(jī)型判斷
- export function isIphoneX() {
- // iPhone X、iPhone XS
- var isIPhoneX =
- /iphone/gi.test(window.navigator.userAgent) &&
- window.devicePixelRatio &&
- window.devicePixelRatio === 3 &&
- window.screen.width === 375 &&
- window.screen.height === 812;
- // iPhone XS Max
- var isIPhoneXSMax =
- /iphone/gi.test(window.navigator.userAgent) &&
- window.devicePixelRatio &&
- window.devicePixelRatio === 3 &&
- window.screen.width === 414 &&
- window.screen.height === 896;
- // iPhone XR
- var isIPhoneXR =
- /iphone/gi.test(window.navigator.userAgent) &&
- window.devicePixelRatio &&
- window.devicePixelRatio === 2 &&
- window.screen.width === 414 &&
- window.screen.height === 896;
- if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) {
- return true;
- }
- return false;
- }
當(dāng)前名稱:前端實(shí)用小工具(URL參數(shù)截取、JSON判斷、數(shù)據(jù)類型檢測(cè)、版本號(hào)對(duì)比等)
網(wǎng)頁(yè)地址:http://www.5511xx.com/article/cdhsdse.html


咨詢
建站咨詢
