Browser.hx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C)2005-2012 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. public static var window(get, never):js.html.DOMWindow;
  27. inline static function get_window() return untyped __js__("window");
  28. public static var document(get, never):js.html.Document;
  29. inline static function get_document() return untyped __js__("window.document");
  30. public static var location(get, never):js.html.Location;
  31. inline static function get_location() return untyped __js__("window.location");
  32. public static var navigator(get, never):js.html.Navigator;
  33. inline static function get_navigator() return untyped __js__("window.navigator");
  34. /**
  35. * True if a window object exists, false otherwise.
  36. *
  37. * This can be used to check if the code is being executed in a non-browser
  38. * environment such as node.js.
  39. */
  40. public static var supported(get, never):Bool;
  41. public static function get_supported() return untyped __js__("typeof window != \"undefined\"");
  42. /**
  43. * Safely gets the browser's local storage, or returns null if localStorage is unsupported or
  44. * disabled.
  45. */
  46. public static function getLocalStorage() : Storage
  47. {
  48. try {
  49. var s = window.localStorage;
  50. s.getItem("");
  51. return s;
  52. } catch( e : Dynamic ) {
  53. return null;
  54. }
  55. }
  56. /**
  57. * Safely gets the browser's session storage, or returns null if sessionStorage is unsupported
  58. * or disabled.
  59. */
  60. public static function getSessionStorage() : Storage
  61. {
  62. try {
  63. var s = window.sessionStorage;
  64. s.getItem("");
  65. return s;
  66. } catch( e : Dynamic ) {
  67. return null;
  68. }
  69. }
  70. /**
  71. * Creates an XMLHttpRequest, with a fallback to ActiveXObject for ancient versions of Internet
  72. * Explorer.
  73. */
  74. public static function createXMLHttpRequest() : XMLHttpRequest
  75. {
  76. if( untyped __js__("typeof XMLHttpRequest") != "undefined" ) {
  77. return new XMLHttpRequest();
  78. }
  79. if( untyped __js__("typeof ActiveXObject") != "undefined" ) {
  80. return untyped __new__("ActiveXObject","Microsoft.XMLHTTP");
  81. }
  82. throw "Unable to create XMLHttpRequest object.";
  83. }
  84. }