1. Native JavaScript implements String Length Truncation
- 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. Obtain the domain name host using native 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. Use native JavaScript to clear spaces
- String.prototype.trim = function() {
- var reExtraSpace = /^\s*(.*?)\s+$/;
- return this.replace(reExtraSpace, "$1")
- }
4. Replace all native JavaScript
- String.prototype.replaceAll = function(s1, s2) {
- return this.replace(new RegExp(s1, "gm"), s2)
- }
5. Native JavaScript escape html tags
- function HtmlEncode(text) {
- return text.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>')
- }
7. Native JavaScript time and date format conversion
- Date. prototype. Format = function (formatStr ){
- Var str = formatStr;
- Var Week = ['day', 'yi', '2', '3', '4', '5', '6'];
- 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/g, (this. getMonth () + 1 ));
- Str = str. replace (/w | W/g, Week [this. getDay ()]);
- Str = str. replace (/dd | DD/, this. getDate ()> 9? This. getDate (). toString (): '0' + this. getDate ());
- Str = str. replace (/d | D/g, this. getDate ());
- Str = str. replace (/hh | HH/, this. getHours ()> 9? This. getHours (). toString (): '0' + this. getHours ());
- Str = str. replace (/h | H/g, this. getHours ());
- Str = str. replace (/mm/, this. getMinutes ()> 9? This. getMinutes (). toString (): '0' + this. getMinutes ());
- Str = str. replace (/m/g, this. getMinutes ());
- Str = str. replace (/ss | SS/, this. getSeconds ()> 9? This. getSeconds (). toString (): '0' + this. getSeconds ());
- Str = str. replace (/s | S/g, this. getSeconds ());
- Return str
- }
8. Native JavaScript checks whether the data type is Numeric
- function isDigit(value) {
- var patrn = /^[0-9]*$/;
- if (patrn.exec(value) == null || value == "") {
- return false
- } else {
- return true
- }
- }
9. Set the cookie value in native JavaScript
- 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);
- document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;"
- }
10. Obtain the cookie value using native JavaScript.
- function getCookie(name) {
- var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
- if (arr != null) return unescape(arr[2]);
- return null
- }
11. Adding 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 add to favorites, please add with Ctrl + D ")
- }
- }
- }
12. Set native JavaScript as the homepage
- 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. To enable this function, enter about: config in the address bar, and set the value of signed. applets. codebase_principal_support to true ")
- }
- }
- Var prefs = Components. classes ['@ mozilla.org/preferences-service%1'}.getservice (Components. interfaces. nsIPrefBranch );
- Prefs. setCharPref ('browser. startup. homepage', 'HTTP: // www.jq-school.com ')
- }
- }