LodopFuncs.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //==本JS是加载Lodop插件及CLodop服务的综合示例,可直接使用,建议看懂后融进自己页面程序==
  2. var CreatedOKLodopObject, CLodopIsLocal, CLodopJsState;
  3. //==判断是否需要CLodop(那些不支持插件的浏览器):==
  4. function needCLodop() {
  5. try {
  6. var ua = navigator.userAgent;
  7. if (ua.match(/Windows\sPhone/i))
  8. return true;
  9. if (ua.match(/iPhone|iPod|iPad/i))
  10. return true;
  11. if (ua.match(/Android/i))
  12. return true;
  13. if (ua.match(/Edge\D?\d+/i))
  14. return true;
  15. var verTrident = ua.match(/Trident\D?\d+/i);
  16. var verIE = ua.match(/MSIE\D?\d+/i);
  17. var verOPR = ua.match(/OPR\D?\d+/i);
  18. var verFF = ua.match(/Firefox\D?\d+/i);
  19. var x64 = ua.match(/x64/i);
  20. if ((!verTrident) && (!verIE) && (x64))
  21. return true;
  22. else if (verFF) {
  23. verFF = verFF[0].match(/\d+/);
  24. if ((verFF[0] >= 41) || (x64))
  25. return true;
  26. } else if (verOPR) {
  27. verOPR = verOPR[0].match(/\d+/);
  28. if (verOPR[0] >= 32)
  29. return true;
  30. } else if ((!verTrident) && (!verIE)) {
  31. var verChrome = ua.match(/Chrome\D?\d+/i);
  32. if (verChrome) {
  33. verChrome = verChrome[0].match(/\d+/);
  34. if (verChrome[0] >= 41)
  35. return true;
  36. }
  37. }
  38. return false;
  39. } catch (err) {
  40. return true;
  41. }
  42. }
  43. //==引用CLodop的主JS,用双端口8000和18000(以防其中一个被占):==
  44. if (needCLodop()) {
  45. var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
  46. var JS1 = document.createElement("script");
  47. JS1.src = "http://localhost:8000/CLodopfuncs.js?priority=1";
  48. head.insertBefore(JS1, head.firstChild);
  49. var JS2 = document.createElement("script");
  50. JS2.src = "http://localhost:18000/CLodopfuncs.js?priority=0";
  51. head.insertBefore(JS2, head.firstChild);
  52. CLodopIsLocal = !!((JS1.src + JS2.src).match(/\/\/localho|\/\/127.0.0./i));
  53. if (JS1.attachEvent) {
  54. CLodopJsState = "loading";
  55. var onChange = function(){
  56. if (window.event.srcElement.readyState == 'loaded')
  57. CLodopJsState = "complete";
  58. };
  59. JS1.attachEvent('onreadystatechange',onChange);
  60. JS2.attachEvent('onreadystatechange',onChange);
  61. }
  62. }
  63. //==获取LODOP对象主过程,判断是否安装、需否升级:==
  64. function getLodop(oOBJECT, oEMBED) {
  65. var strHtmInstall = "<br><font color='#FF00FF'>打印控件未安装!点击这里<a href='install_lodop32.exe' target='_self'>执行安装</a>,安装后请刷新页面或重新进入。</font>";
  66. var strHtmUpdate = "<br><font color='#FF00FF'>打印控件需要升级!点击这里<a href='install_lodop32.exe' target='_self'>执行升级</a>,升级后请重新进入。</font>";
  67. var strHtm64_Install = "<br><font color='#FF00FF'>打印控件未安装!点击这里<a href='install_lodop64.exe' target='_self'>执行安装</a>,安装后请刷新页面或重新进入。</font>";
  68. var strHtm64_Update = "<br><font color='#FF00FF'>打印控件需要升级!点击这里<a href='install_lodop64.exe' target='_self'>执行升级</a>,升级后请重新进入。</font>";
  69. var strHtmFireFox = "<br><br><font color='#FF00FF'>(注意:如曾安装过Lodop旧版附件npActiveXPLugin,请在【工具】->【附加组件】->【扩展】中先卸它)</font>";
  70. var strHtmChrome = "<br><br><font color='#FF00FF'>(如果此前正常,仅因浏览器升级或重安装而出问题,需重新执行以上安装)</font>";
  71. var strCLodopInstall_1 = "<br><font color='#FF00FF'>Web打印服务未安装启动,点击这里<a href='CLodop_Setup_for_Win32NT.exe' target='_self'>下载执行安装</a>";
  72. var strCLodopInstall_2 = "<br>(若此前已安装过,可<a href='CLodop.protocol:setup' target='_self'>点这里直接再次启动</a>)";
  73. var strCLodopInstall_3 = ",成功后请刷新本页面。</font>";
  74. var strCLodopUpdate = "<br><font color='#FF00FF'>Web打印服务CLodop需升级!点击这里<a href='CLodop_Setup_for_Win32NT.exe' target='_self'>执行升级</a>,升级后请刷新页面。</font>";
  75. var LODOP;
  76. try {
  77. var ua = navigator.userAgent;
  78. var isIE = !!(ua.match(/MSIE/i)) || !!(ua.match(/Trident/i));
  79. if (needCLodop()) {
  80. try {
  81. LODOP = getCLodop();
  82. } catch (err) {}
  83. if (!LODOP && (document.readyState !== "complete" || (isIE && CLodopJsState == "loading")) ) {
  84. alert("网页还没下载完毕,请稍等一下再操作.");
  85. return;
  86. }
  87. if (!LODOP) {
  88. document.body.innerHTML = strCLodopInstall_1 + (CLodopIsLocal ? strCLodopInstall_2 : "") + strCLodopInstall_3 + document.body.innerHTML;
  89. return;
  90. } else {
  91. if (CLODOP.CVERSION < "3.0.9.3") {
  92. document.body.innerHTML = strCLodopUpdate + document.body.innerHTML;
  93. }
  94. if (oEMBED && oEMBED.parentNode)
  95. oEMBED.parentNode.removeChild(oEMBED);
  96. if (oOBJECT && oOBJECT.parentNode)
  97. oOBJECT.parentNode.removeChild(oOBJECT);
  98. }
  99. } else {
  100. var is64IE = isIE && !!(ua.match(/x64/i));
  101. //==如果页面有Lodop就直接使用,否则新建:==
  102. if (oOBJECT || oEMBED) {
  103. if (isIE)
  104. LODOP = oOBJECT;
  105. else
  106. LODOP = oEMBED;
  107. } else if (!CreatedOKLodopObject) {
  108. LODOP = document.createElement("object");
  109. LODOP.setAttribute("width", 0);
  110. LODOP.setAttribute("height", 0);
  111. LODOP.setAttribute("style", "position:absolute;left:0px;top:-100px;width:0px;height:0px;");
  112. if (isIE)
  113. LODOP.setAttribute("classid", "clsid:2105C259-1E0C-4534-8141-A753534CB4CA");
  114. else
  115. LODOP.setAttribute("type", "application/x-print-lodop");
  116. document.documentElement.appendChild(LODOP);
  117. CreatedOKLodopObject = LODOP;
  118. } else
  119. LODOP = CreatedOKLodopObject;
  120. //==Lodop插件未安装时提示下载地址:==
  121. if ((!LODOP) || (!LODOP.VERSION)) {
  122. if (ua.indexOf('Chrome') >= 0)
  123. document.body.innerHTML = strHtmChrome + document.body.innerHTML;
  124. if (ua.indexOf('Firefox') >= 0)
  125. document.body.innerHTML = strHtmFireFox + document.body.innerHTML;
  126. document.body.innerHTML = (is64IE ? strHtm64_Install : strHtmInstall) + document.body.innerHTML;
  127. return LODOP;
  128. }
  129. }
  130. if (LODOP.VERSION < "6.2.2.6") {
  131. if (!needCLodop())
  132. document.body.innerHTML = (is64IE ? strHtm64_Update : strHtmUpdate) + document.body.innerHTML;
  133. }
  134. //===如下空白位置适合调用统一功能(如注册语句、语言选择等):==
  135. //=======================================================
  136. return LODOP;
  137. } catch (err) {
  138. alert("getLodop出错:" + err);
  139. }
  140. }