Browser.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C)2005-2019 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. extern inline static function get_window()
  29. return untyped __js__("window");
  30. /** Shortcut to Window.document. */
  31. public static var document(get, never):js.html.HTMLDocument;
  32. extern inline static function get_document()
  33. return window.document;
  34. /** Shortcut to Window.location. */
  35. public static var location(get, never):js.html.Location;
  36. extern inline static function get_location()
  37. return window.location;
  38. /** Shortcut to Window.navigator. */
  39. public static var navigator(get, never):js.html.Navigator;
  40. extern inline static function get_navigator()
  41. return window.navigator;
  42. /** Shortcut to Window.console. */
  43. public static var console(get, never):js.html.ConsoleInstance;
  44. extern inline static function get_console()
  45. return window.console;
  46. /**
  47. * True if a window object exists, false otherwise.
  48. *
  49. * This can be used to check if the code is being executed in a non-browser
  50. * environment such as node.js.
  51. */
  52. public static var supported(get, never):Bool;
  53. extern inline static function get_supported()
  54. return js.Syntax.typeof(window) != "undefined";
  55. /**
  56. * Safely gets the browser's local storage, or returns null if localStorage is unsupported or
  57. * disabled.
  58. */
  59. public static function getLocalStorage():Storage {
  60. try {
  61. var s = window.localStorage;
  62. s.getItem("");
  63. if (s.length == 0) {
  64. var key = "_hx_" + Math.random();
  65. s.setItem(key, key);
  66. s.removeItem(key);
  67. }
  68. return s;
  69. } catch (e:Dynamic) {
  70. return null;
  71. }
  72. }
  73. /**
  74. * Safely gets the browser's session storage, or returns null if sessionStorage is unsupported
  75. * or disabled.
  76. */
  77. public static function getSessionStorage():Storage {
  78. try {
  79. var s = window.sessionStorage;
  80. s.getItem("");
  81. if (s.length == 0) {
  82. var key = "_hx_" + Math.random();
  83. s.setItem(key, key);
  84. s.removeItem(key);
  85. }
  86. return s;
  87. } catch (e:Dynamic) {
  88. return null;
  89. }
  90. }
  91. /**
  92. * Creates an XMLHttpRequest, with a fallback to ActiveXObject for ancient versions of Internet
  93. * Explorer.
  94. */
  95. public static function createXMLHttpRequest():XMLHttpRequest {
  96. if (untyped __js__("typeof XMLHttpRequest") != "undefined") {
  97. return new XMLHttpRequest();
  98. }
  99. if (untyped __js__("typeof ActiveXObject") != "undefined") {
  100. return js.Syntax.construct("ActiveXObject", "Microsoft.XMLHTTP");
  101. }
  102. throw "Unable to create XMLHttpRequest object.";
  103. }
  104. /**
  105. Display an alert message box containing the given message. See also `Window.alert()`.
  106. **/
  107. public static inline function alert(v:Dynamic) {
  108. window.alert(Std.string(v));
  109. }
  110. }