日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
原生JavaScript技巧大收集100(上)

1、原生JavaScript實現(xiàn)字符串長度截取

成都創(chuàng)新互聯(lián)長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為古塔企業(yè)提供專業(yè)的網(wǎng)站制作、成都網(wǎng)站制作,古塔網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

 
 
 
  1. function cutstr(str, len) { 
  2.     var temp; 
  3.     var icount = 0; 
  4.     var patrn = /[^\x00-\xff]/; 
  5.     var strre = ""; 
  6.     for (var i = 0; i < str.length; i++) { 
  7.         if (icount < len - 1) { 
  8.             temp = str.substr(i, 1); 
  9.             if (patrn.exec(temp) == null) { 
  10.                 icount = icount + 1 
  11.             } else { 
  12.                 icount = icount + 2 
  13.             } 
  14.             strre += temp 
  15.         } else { 
  16.             break 
  17.         } 
  18.     } 
  19.     return strre + "..." 

2、原生JavaScript獲取域名主機

 
 
 
  1. function getHost(url) { 
  2.     var host = "null"; 
  3.     if(typeof url == "undefined"|| null == url) { 
  4.         url = window.location.href; 
  5.     } 
  6.     var regex = /^\w+\:\/\/([^\/]*).*/; 
  7.     var match = url.match(regex); 
  8.     if(typeof match != "undefined" && null != match) { 
  9.         host = match[1]; 
  10.     } 
  11.     return host; 

3、原生JavaScript清除空格

 
 
 
  1. String.prototype.trim = function() { 
  2.     var reExtraSpace = /^\s*(.*?)\s+$/; 
  3.     return this.replace(reExtraSpace, "$1") 

4、原生JavaScript替換全部

 
 
 
  1. String.prototype.replaceAll = function(s1, s2) { 
  2.     return this.replace(new RegExp(s1, "gm"), s2) 

5、原生JavaScript轉(zhuǎn)義html標簽

 
 
 
  1. function HtmlEncode(text) { 
  2.     return text.replace(/&/g, '&').replace(/\"/g, '"').replace(//g, '>') 

7、原生JavaScript時間日期格式轉(zhuǎn)換

 
 
 
  1. Date.prototype.Format = function(formatStr) { 
  2.     var str = formatStr; 
  3.     var Week = ['日', '一', '二', '三', '四', '五', '六']; 
  4.     str = str.replace(/yyyy|YYYY/, this.getFullYear()); 
  5.     str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100)); 
  6.     str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1)); 
  7.     str = str.replace(/M/g, (this.getMonth() + 1)); 
  8.     str = str.replace(/w|W/g, Week[this.getDay()]); 
  9.     str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate()); 
  10.     str = str.replace(/d|D/g, this.getDate()); 
  11.     str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours()); 
  12.     str = str.replace(/h|H/g, this.getHours()); 
  13.     str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes()); 
  14.     str = str.replace(/m/g, this.getMinutes()); 
  15.     str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds()); 
  16.     str = str.replace(/s|S/g, this.getSeconds()); 
  17.     return str 

8、原生JavaScript判斷是否為數(shù)字類型

 
 
 
  1. function isDigit(value) { 
  2.     var patrn = /^[0-9]*$/; 
  3.     if (patrn.exec(value) == null || value == "") { 
  4.         return false 
  5.     } else { 
  6.         return true 
  7.     } 

9、原生JavaScript設(shè)置cookie值

 
 
 
  1. function setCookie(name, value, Hours) { 
  2.     var d = new Date(); 
  3.     var offset = 8; 
  4.     var utc = d.getTime() + (d.getTimezoneOffset() * 60000); 
  5.     var nd = utc + (3600000 * offset); 
  6.     var exp = new Date(nd); 
  7.     exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); 
  8.     document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;" 

10、原生JavaScript獲取cookie值

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

11、原生JavaScript加入收藏夾

 
 
 
  1. function AddFavorite(sURL, sTitle) { 
  2.     try { 
  3.         window.external.addFavorite(sURL, sTitle) 
  4.     } catch(e) { 
  5.         try { 
  6.             window.sidebar.addPanel(sTitle, sURL, "") 
  7.         } catch(e) { 
  8.             alert("加入收藏失敗,請使用Ctrl+D進行添加") 
  9.         } 
  10.     } 

12、原生JavaScript設(shè)為首頁

 
 
 
  1. function setHomepage() { 
  2.     if (document.all) { 
  3.         document.body.style.behavior = 'url(#default#homepage)'; 
  4.         document.body.setHomePage('http://www.jq-school.com') 
  5.     } else if (window.sidebar) { 
  6.         if (window.netscape) { 
  7.             try { 
  8.                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect") 
  9.             } catch(e) { 
  10.                 alert("該操作被瀏覽器拒絕,如果想啟用該功能,請在地址欄內(nèi)輸入 about:config,然后將項 signed.applets.codebase_principal_support 值該為true") 
  11.             } 
  12.         } 
  13.         var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch); 
  14.         prefs.setCharPref('browser.startup.homepage', 'http://www.jq-school.com') 
  15.     } 

#p#

13、原生JavaScript判斷IE6

 
 
 
  1. var ua = navigator.userAgent.toLowerCase(); 
  2. var isIE6 = ua.indexOf("msie 6") > -1; 
  3. if (isIE6) { 
  4.     try { 
  5.         document.execCommand("BackgroundImageCache", false, true) 
  6.     } catch(e) {} 

14、原生JavaScript加載樣式文件

 
 
 
  1. function LoadStyle(url) { 
  2.     try { 
  3.         document.createStyleSheet(url) 
  4.     } catch(e) { 
  5.         var cssLink = document.createElement('link'); 
  6.         cssLink.rel = 'stylesheet'; 
  7.         cssLink.type = 'text/css'; 
  8.         cssLink.href = url; 
  9.         var head = document.getElementsByTagName('head')[0]; 
  10.         head.appendChild(cssLink) 
  11.     } 

15、原生JavaScript返回腳本內(nèi)容

 
 
 
  1. function evalscript(s) { 
  2.         if(s.indexOf('