首页 > 代码库 > javascript设计模式学习笔记
javascript设计模式学习笔记
1、有一些比较常用的代码可以作为积累,这样在实现某个功能的时候能更快的达到目的,学习基础是为了看懂,但是要能应用除了自己写,也应该积累比较好的代码:
逐步尝试每一种XHR对象,直到遇到当前的javascript环境支持的对象为止。
/ SimpleXhrFactory singleton, step 1. /
var SimpleXhrFactory = (function() {
// The three branches.
var standard = {
createXhrObject: function() { return new XMLHttpRequest(); }
};
var activeXNew = {
createXhrObject: function() { return new ActiveXObject(‘Msxml2.XMLHTTP‘); }
};
var activeXOld = {
createXhrObject: function() { return new ActiveXObject(‘Microsoft.XMLHTTP‘); }
};
})();
/ SimpleXhrFactory singleton, step 2. /
var SimpleXhrFactory = (function() {
// The three branches.
var standard = {
createXhrObject: function() { return new XMLHttpRequest(); }
};
var activeXNew = {
createXhrObject: function() { return new ActiveXObject(‘Msxml2.XMLHTTP‘); }
};
var activeXOld = {
createXhrObject: function() { return new ActiveXObject(‘Microsoft.XMLHTTP‘); }
};
// To assign the branch, try each method; return whatever doesn‘t fail.
var testObject;
try {
testObject = standard.createXhrObject(); return standard; // Return this if no error was thrown.
}
catch(e) {
try { testObject = activeXNew.createXhrObject(); return activeXNew; // Return this if no error was thrown. } catch(e) { try { testObject = activeXOld.createXhrObject(); return activeXOld; // Return this if no error was thrown. } catch(e) { throw new Error(‘No XHR object found in this environment.‘); } }
}
})();
2、关于获取当前浏览器信息的常用方法:
//=========
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)
document.write(“浏览器名称:“+ browser)
document.write(“
“)
document.write(“浏览器版本:“+ version)
//============
document.write(“
代码:“)
document.write(navigator.appCodeName + “
document.write(“
平台:“)
document.write(navigator.platform + “
document.write(“
Cookies 启用:“)
document.write(navigator.cookieEnabled + “
document.write(“
浏览器的用户代理报头:“)
document.write(navigator.userAgent + “
//=========
var x = navigator;
document.write(“CodeName=” + x.appCodeName);
document.write(“
“);
document.write(“MinorVersion=” + x.appMinorVersion);
document.write(“
“);
document.write(“Name=” + x.appName);
document.write(“
“);
document.write(“Version=” + x.appVersion);
document.write(“
“);
document.write(“CookieEnabled=” + x.cookieEnabled);
document.write(“
“);
document.write(“CPUClass=” + x.cpuClass);
document.write(“
“);
document.write(“OnLine=” + x.onLine);
document.write(“
“);
document.write(“Platform=” + x.platform);
document.write(“
“);
document.write(“UA=” + x.userAgent);
document.write(“
“);
document.write(“BrowserLanguage=” + x.browserLanguage);
document.write(“
“);
document.write(“SystemLanguage=” + x.systemLanguage);
document.write(“
“);
document.write(“UserLanguage=” + x.userLanguage);
javascript设计模式学习笔记