PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/net/manaca/utils/URLUtils.as

https://github.com/wersling/manaca
ActionScript | 210 lines | 97 code | 5 blank | 108 comment | 26 complexity | 31223e45d2803a0472722874d0ad946a MD5 | raw file
  1. package net.manaca.utils
  2. {
  3. import flash.external.ExternalInterface;
  4. import flash.net.URLRequest;
  5. import flash.net.navigateToURL;
  6. /**
  7. * Utility for creating pop-up windows, which helps you to launch pop-ups
  8. * without restriction
  9. *
  10. * ActionScript 3.0 / Flash 9
  11. *
  12. * Note:
  13. * 1. Pop-up type
  14. * In Flash, the pop-ups will be treated as two different approaches
  15. * in browsers.
  16. * • Mouse-click pop-ups, Flash opens a window with a Mouse-click
  17. * • Auto-launch pop-ups, Flash opens pop-ups without a Mouse-click
  18. *
  19. * 2. Flash Code
  20. * *NOTE*: we have to use the MouseEvent.CLICK for capturing Mouse
  21. * Event while opening a window. The MouseEvent.MOUSE_DOWN doesn't work
  22. * on some of browsers.
  23. *
  24. * 3. Flash Object
  25. * 1. In Firefox,
  26. * In order for the Firefox to trust the Flash object, thus allow popup
  27. * windows, we need to set the wmode property of the Flash object to
  28. * "opaque" or "transparent" (obviously don't use transparent unless we
  29. * really need to). This is required when working together with the
  30. * Flash player \>= 9.0.115, even for Mouse-click pop-ups.
  31. *
  32. * However, if we want to use full screen feature in Flash, we need to
  33. * set the wmode to "window" in the following situation: Flash player >
  34. * 9.0.115 and non-IE browsers. Fortunately all pop-ups work in this
  35. * situation after tested.
  36. *
  37. * 2. In Internet Explorer,
  38. * We need to set the wmode property of the Flash object to "window" in
  39. * order to make the Auto-launch pop-ups work.
  40. *
  41. * *Conclusions:*
  42. * If we don’t need Flash full screen and Auto-launch pop-ups in
  43. * project, we can simply set the wmode property of the Flash object to
  44. * "opaque". This approach would be able to help us to prevent all
  45. * Mouse-click pop-ups blocking.
  46. *
  47. * If we need either Flash full screen or Auto-launch pop-ups (in
  48. * Internet Explorer), we can set the wmode property of the Flash
  49. * object to "window" as default. Then set the wmode property to
  50. * “opaque”, if it’s running in Flash players \>= 9.0.115 and non-IE
  51. * browsers. The JavaScript of checking Flash player and browsers in
  52. * Flash.js could be like the following:
  53. *
  54. * Additionally, we need to set the allowScriptAccess property of the
  55. * Flash object to "always" if Flash object and Flash wrapper are
  56. * hosted in the different domains.
  57. *
  58. * One more thing: our Flash object will need to have an id in order
  59. * for the ExternalInterface class to work.
  60. *
  61. * 4. Test report
  62. * The Mouse-click pop-ups are tested on the following browsers with
  63. * Flash Player 9.0.28 - 9.0.124:
  64. * • Internet Explorer 6.0 - Windows
  65. * • Internet Explorer 7.0 - Windows
  66. * • Firefox 2.0 - Windows
  67. * • Firefox 3.0 - Windows
  68. * • Opera 9.5 - Windows
  69. * • Safari 3.1.1 - Mac
  70. *
  71. * The Auto-launch pop-ups are not allowed *only* in the following
  72. * configurations:
  73. * • Flash Player 9.0.115 - Firefox 2.0
  74. * • Flash Player 9.0.115 - Firefox 3.0
  75. *
  76. * based on script by
  77. * @author Philipp Kyeck / phil@apdevblog.com
  78. * @see http://apdevblog.com/problems-using-navigatetourl/
  79. *
  80. * based on script by
  81. * @author Sergey Kovalyov
  82. * @see http://skovalyov.blogspot.com/2007/01/
  83. * how-to-prevent-pop-up-blocking-in.html
  84. *
  85. * and based on script by
  86. * @author Jason the Saj
  87. * @see http://thesaj.wordpress.com/2008/02/12/
  88. * the-nightmare-that-is-_blank-part-ii-help
  89. */
  90. public class URLUtils
  91. {
  92. static private const WINDOW_OPEN_FUNCTION:String = "window.open";
  93. static private var browserName:String;
  94. /**
  95. * Open a new browser window and prevent browser from blocking it.
  96. *
  97. * @param url url to be opened
  98. * @param window window target
  99. * @param features additional features for window.open function
  100. */
  101. static public function openWindow(url:String, window:String = "_blank",
  102. features:String = ""):void
  103. {
  104. if(!browserName)
  105. {
  106. browserName = getBrowserName();
  107. }
  108. //If Firefox
  109. if(browserName == Browser.Firefox)
  110. {
  111. ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window,
  112. features);
  113. }
  114. //If IE
  115. else if(browserName == Browser.MSIE)
  116. {
  117. ExternalInterface.call("function setWMWindow() {"
  118. + WINDOW_OPEN_FUNCTION + "('" + url + "', '"
  119. + window + "', '" + features + "');}");
  120. }
  121. //If Safari
  122. else if(browserName == Browser.Safari)
  123. {
  124. navigateToURL(new URLRequest(url), window);
  125. }
  126. //If Opera
  127. else if(browserName == Browser.Opera)
  128. {
  129. navigateToURL(new URLRequest(url), window);
  130. }
  131. //Otherwise, use Flash's native 'navigateToURL()' function to pop-up
  132. //window. This is necessary because Safari 3 no longer works with
  133. //the above ExternalInterface work-a-round.
  134. else
  135. {
  136. navigateToURL(new URLRequest(url), window);
  137. }
  138. }
  139. static public function clearCacheUrl(url:String):String
  140. {
  141. if(url.indexOf("?") != -1)
  142. {
  143. url += "&temp=" + new Date().getTime();
  144. }
  145. else
  146. {
  147. url += "?temp=" + new Date().getTime();
  148. }
  149. return url;
  150. }
  151. /**
  152. * @return current browser name.
  153. */
  154. static private function getBrowserName():String
  155. {
  156. var browser:String;
  157. var browserAgent:String;
  158. //Uses external interface to reach out to browser and grab browser
  159. //useragent info.
  160. if(ExternalInterface.available)
  161. {
  162. browserAgent = ExternalInterface.call(
  163. "function getBrowser(){return navigator.userAgent;}");
  164. }
  165. //Determines brand of browser using a find index. If not found
  166. //indexOf returns (-1).
  167. if(browserAgent != null &&
  168. browserAgent.indexOf(Browser.Firefox) >= 0)
  169. {
  170. browser = Browser.Firefox;
  171. }
  172. else if(browserAgent != null &&
  173. browserAgent.indexOf(Browser.Safari) >= 0)
  174. {
  175. browser = Browser.Safari;
  176. }
  177. else if(browserAgent != null &&
  178. browserAgent.indexOf(Browser.MSIE) >= 0)
  179. {
  180. browser = Browser.MSIE;
  181. }
  182. else if(browserAgent != null &&
  183. browserAgent.indexOf(Browser.Opera) >= 0)
  184. {
  185. browser = Browser.Opera;
  186. }
  187. else
  188. {
  189. browser = Browser.Undefined;
  190. }
  191. return browser;
  192. }
  193. }
  194. }
  195. /**
  196. * Define a list of browsers
  197. */
  198. class Browser
  199. {
  200. static public const MSIE:String = "MSIE";
  201. static public const Firefox:String = "Firefox";
  202. static public const Safari:String = "Safari";
  203. static public const Opera:String = "Opera";
  204. static public const Undefined:String = "Undefined";
  205. }