jscript.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Detection of JScript version.
  16. *
  17. * @author arv@google.com (Erik Arvidsson)
  18. */
  19. goog.provide('goog.userAgent.jscript');
  20. goog.require('goog.string');
  21. /**
  22. * @define {boolean} True if it is known at compile time that the runtime
  23. * environment will not be using JScript.
  24. */
  25. goog.define('goog.userAgent.jscript.ASSUME_NO_JSCRIPT', false);
  26. /**
  27. * Initializer for goog.userAgent.jscript. Detects if the user agent is using
  28. * Microsoft JScript and which version of it.
  29. *
  30. * This is a named function so that it can be stripped via the jscompiler
  31. * option for stripping types.
  32. * @private
  33. */
  34. goog.userAgent.jscript.init_ = function() {
  35. var hasScriptEngine = 'ScriptEngine' in goog.global;
  36. /**
  37. * @type {boolean}
  38. * @private
  39. */
  40. goog.userAgent.jscript.DETECTED_HAS_JSCRIPT_ =
  41. hasScriptEngine && goog.global['ScriptEngine']() == 'JScript';
  42. /**
  43. * @type {string}
  44. * @private
  45. */
  46. goog.userAgent.jscript.DETECTED_VERSION_ =
  47. goog.userAgent.jscript.DETECTED_HAS_JSCRIPT_ ?
  48. (goog.global['ScriptEngineMajorVersion']() + '.' +
  49. goog.global['ScriptEngineMinorVersion']() + '.' +
  50. goog.global['ScriptEngineBuildVersion']()) :
  51. '0';
  52. };
  53. if (!goog.userAgent.jscript.ASSUME_NO_JSCRIPT) {
  54. goog.userAgent.jscript.init_();
  55. }
  56. /**
  57. * Whether we detect that the user agent is using Microsoft JScript.
  58. * @type {boolean}
  59. */
  60. goog.userAgent.jscript.HAS_JSCRIPT = goog.userAgent.jscript.ASSUME_NO_JSCRIPT ?
  61. false :
  62. goog.userAgent.jscript.DETECTED_HAS_JSCRIPT_;
  63. /**
  64. * The installed version of JScript.
  65. * @type {string}
  66. */
  67. goog.userAgent.jscript.VERSION = goog.userAgent.jscript.ASSUME_NO_JSCRIPT ?
  68. '0' :
  69. goog.userAgent.jscript.DETECTED_VERSION_;
  70. /**
  71. * Whether the installed version of JScript is as new or newer than a given
  72. * version.
  73. * @param {string} version The version to check.
  74. * @return {boolean} Whether the installed version of JScript is as new or
  75. * newer than the given version.
  76. */
  77. goog.userAgent.jscript.isVersion = function(version) {
  78. return goog.string.compareVersions(goog.userAgent.jscript.VERSION, version) >=
  79. 0;
  80. };