Browser.hx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * Safely gets the browser's local storage, or returns null if localStorage is unsupported or
  36. * disabled.
  37. */
  38. public static function getLocalStorage() : Storage
  39. {
  40. try {
  41. var s = window.localStorage;
  42. s.getItem("");
  43. return s;
  44. } catch( e : Dynamic ) {
  45. return null;
  46. }
  47. }
  48. /**
  49. * Safely gets the browser's session storage, or returns null if sessionStorage is unsupported
  50. * or disabled.
  51. */
  52. public static function getSessionStorage() : Storage
  53. {
  54. try {
  55. var s = window.sessionStorage;
  56. s.getItem("");
  57. return s;
  58. } catch( e : Dynamic ) {
  59. return null;
  60. }
  61. }
  62. /**
  63. * Creates an XMLHttpRequest, with a fallback to ActiveXObject for ancient versions of Internet
  64. * Explorer.
  65. */
  66. public static function createXMLHttpRequest() : XMLHttpRequest
  67. {
  68. if( untyped __js__("typeof XMLHttpRequest") != "undefined" ) {
  69. return new XMLHttpRequest();
  70. }
  71. if( untyped __js__("typeof ActiveXObject") != "undefined" ) {
  72. return untyped __new__("ActiveXObject","Microsoft.XMLHTTP");
  73. }
  74. throw "Unable to create XMLHttpRequest object.";
  75. }
  76. }