Browser.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js;
  23. import js.html.Storage;
  24. import js.html.XMLHttpRequest;
  25. class Browser {
  26. /** The global window object. */
  27. public static var window(get, never):js.html.Window;
  28. inline static function get_window() return untyped __js__("window");
  29. /** Shortcut to Window.document. */
  30. public static var document(get, never):js.html.HTMLDocument;
  31. inline static function get_document() return untyped __js__("window.document");
  32. /** Shortcut to Window.location. */
  33. public static var location(get, never):js.html.Location;
  34. inline static function get_location() return untyped __js__("window.location");
  35. /** Shortcut to Window.navigator. */
  36. public static var navigator(get, never):js.html.Navigator;
  37. inline static function get_navigator() return untyped __js__("window.navigator");
  38. /** Shortcut to Window.console. */
  39. public static var console(get, never):js.html.Console;
  40. inline static function get_console() return untyped __js__("window.console");
  41. /**
  42. * True if a window object exists, false otherwise.
  43. *
  44. * This can be used to check if the code is being executed in a non-browser
  45. * environment such as node.js.
  46. */
  47. public static var supported(get, never):Bool;
  48. inline static function get_supported() return untyped __typeof__(window) != "undefined";
  49. /**
  50. * Safely gets the browser's local storage, or returns null if localStorage is unsupported or
  51. * disabled.
  52. */
  53. public static function getLocalStorage() : Storage
  54. {
  55. try {
  56. var s = window.localStorage;
  57. s.getItem("");
  58. if (s.length == 0) {
  59. var key = "_hx_" + Math.random();
  60. s.setItem(key,key);
  61. s.removeItem(key);
  62. }
  63. return s;
  64. } catch( e : Dynamic ) {
  65. return null;
  66. }
  67. }
  68. /**
  69. * Safely gets the browser's session storage, or returns null if sessionStorage is unsupported
  70. * or disabled.
  71. */
  72. public static function getSessionStorage() : Storage
  73. {
  74. try {
  75. var s = window.sessionStorage;
  76. s.getItem("");
  77. if (s.length == 0) {
  78. var key = "_hx_" + Math.random();
  79. s.setItem(key,key);
  80. s.removeItem(key);
  81. }
  82. return s;
  83. } catch( e : Dynamic ) {
  84. return null;
  85. }
  86. }
  87. /**
  88. * Creates an XMLHttpRequest, with a fallback to ActiveXObject for ancient versions of Internet
  89. * Explorer.
  90. */
  91. public static function createXMLHttpRequest() : XMLHttpRequest
  92. {
  93. if( untyped __js__("typeof XMLHttpRequest") != "undefined" ) {
  94. return new XMLHttpRequest();
  95. }
  96. if( untyped __js__("typeof ActiveXObject") != "undefined" ) {
  97. return untyped __new__("ActiveXObject","Microsoft.XMLHTTP");
  98. }
  99. throw "Unable to create XMLHttpRequest object.";
  100. }
  101. /**
  102. Display an alert message box containing the given message. See also `Window.alert()`.
  103. **/
  104. public static function alert( v : Dynamic ) {
  105. @:privateAccess window.alert(Boot.__string_rec(v,""));
  106. }
  107. }