Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the native JavaScript skills?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the native JavaScript skills". In the daily operation, I believe many people have doubts about the native JavaScript skills. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions about "what are the native JavaScript skills?" Next, please follow the editor to study!

1. Native JavaScript implements string length interception

Function cutstr (str, len) {var temp; var icount = 0; var patrn = / [^\ X00 -\ xff] /; var strre = ""; for (var I = 0; I

< str.length; i++) { if (icount < len - 1) { temp = str.substr(i, 1); if (patrn.exec(temp) == null) { icount = icount + 1 } else { icount = icount + 2 } strre += temp } else { break } } return strre + "..." } 2、原生JavaScript获取域名主机 function getHost(url) { var host = "null"; if(typeof url == "undefined"|| null == url) { url = _window.location.href; } var regex = /^\w+\:\/\/([^\/]*).*/; var match = url.match(regex); if(typeof match != "undefined" && null != match) { host = match[1]; } return host; } 3、原生JavaScript清除空格 String.prototype.trim = function() { var reExtraSpace = /^\s*(.*?)\s+$/; return this.replace(reExtraSpace, "$1") } 4、原生JavaScript替换全部 String.prototype.replaceAll = function(s1, s2) { return this.replace(new RegExp(s1, "gm"), s2) } 5、原生JavaScript转义html标签 function HtmlEncode(text) { return text.replace(/&/g, '&').replace(/\"/g, '"').replace(//g, '>

')}

7. Native JavaScript time date format conversion

Date.prototype.Format = function (formatStr) {var str = formatStr; var Week = ['Day', 'one', 'two', 'three', 'four', 'five', 'six']; str = str.replace (/ yyyy | YYYY/, this.getFullYear ()); str = str.replace (/ yy | YY/, (this.getYear () 100) > 9? (this.getYear ()% 100) .toString ():'0' + (this.getYear ()% 100)); str = str.replace (/ MM/, (this.getMonth () + 1) > 9? (this.getMonth () + 1). ToString ():'0' + (this.getMonth () + 1)); str = str.replace (/ M str.replace g, (this.getMonth () + 1)); str = str.replace (/ w | Whand g, Week [this.getDay ()]); str = str.replace (/ dd | DD/, this.getDate () > 9? This.getDate (). ToString ():'0' + this.getDate (); str = str.replace (/ d | DWeig, this.getDate ()); str = str.replace (/ hh | HH/, this.getHours () > 9? This.getHours (). ToString ():'0' + this.getHours (); str = str.replace (/ h | HWeig, this.getHours ()); str = str.replace (/ mm/, this.getMinutes () > 9? This.getMinutes (). ToString ():'0' + this.getMinutes (); str = str.replace (/ m _ Unig, this.getMinutes ()); str = str.replace (/ ss | SS/, this.getSeconds () > 9? This.getSeconds () .toString ():'0' + this.getSeconds (); str = str.replace (/ s | SWeig, this.getSeconds ()); return str}

8. Native JavaScript determines whether it is a numeric type.

Function isDigit (value) {var patrn = / ^ [0-9] * $/; if (patrn.exec (value) = = null | | value = "") {return false} else {return true}}

9. Native JavaScript sets cookie value

Function setCookie (name, value, Hours) {var d = new Date (); var offset = 8; var utc = d.getTime () + (d.getTimezoneOffset () * 60000); var nd = utc + (3600000 * offset); var exp = new Date (nd); exp.setTime (exp.getTime () + Hours * 60 * 60 * 1000); [xss_clean] = name + "=" + escape (value) +; path=/;expires= "+ exp.toGMTString () +" Domain=360doc.com; "}

10. Native JavaScript gets cookie value

Function getCookie (name) {var arr = [xss_clean] .match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $)"); if (arr! = null) return unescape (arr [2]); return null}

11. Add native JavaScript to favorites

Function AddFavorite (sURL, sTitle) {try {window.external.addFavorite (sURL, sTitle)} catch (e) {try {window.sidebar.addPanel (sTitle, sURL, "")} catch (e) {alert ("failed to join the collection, please use Ctrl+D to add")}

12. The native JavaScript is set as the home page

Function setHomepage () {if (document.all) {document.body.style.behavior = 'url (# default#homepage)' Document.body.setHomePage ('http://www.jq-school.com')} else if (window.sidebar) {if (window.netscape) {try {netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect")} catch (e) {alert ("this operation is rejected by the browser, if you want to enable this feature Please enter about:config in the address bar, and then set the signed.applets.codebase_principal_support value as true ")}} var prefs = Components.classes ['@ mozilla.org/preferences-service" 1'] .getService (Components.interfaces.nsIPrefBranch); prefs.setCharPref ('browser.startup.homepage',' http://www.jq-school.com')}})

13. Native JavaScript judges IE6

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

14. Native JavaScript load style files

Function LoadStyle (url) {try {document.createStyleSheet (url)} catch (e) {var cssLink = document.createElement ('link'); cssLink.rel =' stylesheet'; cssLink.type = 'text/css'; cssLink.href = url; var head = document.getElementsByTagName (' head') [0]; head.appendChild (cssLink)}}

15. Native JavaScript returns script content

Function evalscript (s) {if (s.indexOf ('

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report