Home > Web Front-end > JS Tutorial > body text

Some thoughts on standardization in JavaScript development_javascript skills

WBOY
Release: 2016-05-16 18:50:59
Original
1301 people have browsed it

It can be said to be done once and for all, don’t reinvent the wheel:)
1. Commonly used methods are placed uniformly
For example: when a user registers, it is often necessary to determine whether the characters in the text box are Chinese characters, English, numbers or Email address, etc. Why not put these methods into a script and call it utility.js?

Copy code The code is as follows:

//Save it as a js when needed

function isNull(obj)
{
if (!obj || obj.length==0 || obj=="")
{
parent.MyAlert("The label name cannot be Empty!",alertImg);
return false;
}
else
{
return true;
}
}

// Verify whether For integers
function isNumber(oNum)
{
if(!oNum) return false;

try{
if(parseInt(oNum)!=oNum) {
parent.MyAlert("Please fill in a positive integer to query the distance!",alertImg);
return false;
}
}
catch(ex)
{
parent.MyAlert(" Please fill in a positive integer to query the distance!",alertImg);
return false;
}

return true;
}

// Verify whether it is Chinese
function isChinese(oCn)
{
if (!oCn || oCn.length==0) return false;

try{
var reg = /^[u4e00-u9fa5] $ /i; //Including Chinese

if ( reg.test(oCn) )
{
return true;
}
else{
parent.MyAlert("Key Please fill in Chinese for the keyword!",alertImg);
return false;
}
}
catch(ex)
{
parent.MyAlert("Please fill in Chinese for the keyword!" ,alertImg);
return false;
}
}

// Verify whether it is Chinese or letters
function isEnCh(oStr)
{
if (! oStr || oStr.length==0) return false;

try{
var reg = /^[a-zA-Zu4E00-u9FA5]/g; //Contains Chinese or Pinyin

if ( reg.test(oStr) )
{
return true;
}
else{
parent.MyAlert("Please fill in Chinese or Pinyin for the place name!",alertImg) ;
return false;
}
}
catch(ex)
{
parent.MyAlert("Please fill in Chinese or Pinyin for the place name!",alertImg);
return false;
}
}

// The result retains 2 decimal places
function roundAmount(n){
var s = "" Math.round(n * 100) / 100 ;
var i = s.indexOf('.')
if (i < 0) return s ".00";

var t = s.substring(0, i 1) s.substring(i 1, i 3);
if (i 2 == s.length) t = "0";

return t;
}

2. Prompts and error messages are placed together

After reading the above code, do you also find a problem: if the prompts and error messages are different in the next project, you need to re- Change the above code. Why not put the prompt information in a script and call it resource_zh.js?

In multi-language versions of software, similar tricks are also used to switch interface languages. Have you found any? hehe.
Copy code The code is as follows:

var page_res = {
"meter": " "meter",
"kilometer": "kilometer",
"mile": "mile",
"yard": "yard",
"degree": "degree" ,
"millimeter": "Millimeter",
"saveMap": "Save map",
"printMap": "Print map",
"queryResult": "Query results",
"queryResultNull": "The query result is empty",
"experssionCanNotNull": "The query expression cannot be empty",
"networkInfo": "Path information",
"arcInfo": "Arc segment "Message",
"addEntitySuccess": "Add entity successfully!",
"addEntityFail": "Add entity failed!",
"updateEntitySuccess": "Update entity successfully!",
"updateEntityFail": "Update feature failed!",
"updatePropertySuccess": "Update property successfully!",
"updatePropertyFail": "Update property failed!",
"deleteEntitySuccess": "Delete Feature successful!",
"deleteEntityFail": "Delete feature failed!",
"closestFacilityFail": "Recent facility analysis failed, please select again",
"fieldCanNotNull": "The number of fields cannot be is empty",
"fieldMustInteger": "The number of fields must be an integer",
"fieldMustMoreThanZero": "The number of fields must be greater than 0",
"numberCanNotNull": "The number cannot be empty"
}

We can put commonly used units, button text, and prompt information here. Then load the script first and instantiate an object through eval. The corresponding content can be obtained through something like res.networkInfo.

3. AjaxRequest request encapsulation

is consistent with the first point. Ajax is frequently used in current development. If you do not use script libraries such as jQuery, It may be that you write the functions for each request and callback yourself. Why not put all these methods in an ajax.js?
Copy code The code is as follows:

var xmlhttpObj=false;
function XHR(CallBack )
{
this.callback=CallBack;
}
XHR.createXMLHttp = function ()
{
if(window.ActiveXObject) //IE browser
{
try
{
xmlhttpObj = new ActiveXObject("Microsoft.XMLHTTP");//IE4.0
}
catch (e)
{
try
{
xmlhttpObj = new ActiveXObject("Msxml2.XMLHTTP");//IE5.0 or above
}
catch (e2)
{
xmlhttpObj=false;
}
}
}
else if(window.XMLHttpRequest&&!xmlhttpObj) //Open browser
{
xmlhttpObj = new XMLHttpRequest();
}
}
XHR. prototype.Onstar = function (method,Url,bFlag,param)
{
if(this.callback!=null)
{
XHR.createXMLHttp();
xmlhttpObj.onreadystatechange= this.callback;
xmlhttpObj.open(method,Url,bFlag);
xmlhttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlhttpObj. send(param);
}
else
{
alert("No client handler!")
}
}

in use Instantiate an XHR object, for example: var legendObj=new XHR(function (){...}); Then use legendObj.Onstar("POST","Handlers/legendHandler.ashx",false,"mapName=" mapName); just submit the request.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template