Javascript class, namespace, Code Organization Code. For details about js learning, refer.
The Code is as follows:
$ (Function (){
PageJs. urls = [
'/', 'Index ',
];
PageJs. run ();
});
// The functions include html elements. Ajax function also can be put here.
Var Common = {
Init: function (){
Alert ('common ');
}
};
// Url -- pages
Var Index = {
Init: function (){
Common. init ();
This. test ();
This. test2 ();
Alert (location. pathname );
},
Test: function (){
Alert ('test ');
}
};
Index. test2 = function (){
Index. test ();
Alert ('test2 ');
}
// Shocould be remove to other js file, and load before this file.
// The functions don't include any html elements.
Var Utils = {
Debug: function (){
}
};
Var pageJs = {
Urls: [],
Run: function (){
Var urls = this. urls;
Alert (urls. length );
If (urls. length % 2 )! = 0 ){
Throw "urls error ";
}
Var len = urls. length/2;
For (var I = 0; I <= len; I = I + 2 ){
Var pattern = new RegExp (urls [I], 'I ');
If (pattern. test (location. pathname )){
Eval (urls [I + 1] + ". init ()");
Break;
}
}
}
};