Browser.hx 2.8 KB

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