旅途笔记

岂有豪情似旧时,花开花落两由之
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

as3 打开窗口类

Posted on 2016-06-03 00:11  allonkwok  阅读(197)  评论(0编辑  收藏  举报
 package FlashCode.utils{
 
 import flash.display.Sprite;
 import flash.net.URLRequest;
 import flash.net.navigateToURL;
 import flash.external.ExternalInterface;
 
 public class openWindow {
  public function openWindow(url:String, window:String="_blank", features:String=""){
     
   var WINDOW_OPEN_FUNCTION:String = "window.open";
   var myURL:URLRequest = new URLRequest(url);   
   var browserName:String = getBrowserName();
   
   if(getBrowserName() == "Firefox"){
    ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
   }
   //If IE, 
   else if(browserName == "IE"){
    ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
    //ExternalInterface.call("function setWMWindow() {window.open('" + url + "');}");
   }
   //If Safari 
   else if(browserName == "Safari"){     
    navigateToURL(myURL, window);
   }
   //If Opera 
   else if(browserName == "Opera"){ 
    navigateToURL(myURL, window); 
   } else {
    navigateToURL(myURL, window);
   }
   
   /*Alternate methodology...
      var popSuccess:Boolean = ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features); 
   if(popSuccess == false){
    navigateToURL(myURL, window);
   }*/
    
  }
  private function getBrowserName():String{
   var browser:String;
   
   //Uses external interface to reach out to browser and grab browser useragent info.
   var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
  
    //  Debug.text += "Browser Info: [" + browserAgent + "]";
   
   //Determines brand of browser using a find index. If not found indexOf returns (-1).
   if(browserAgent != null && browserAgent.indexOf("Firefox") >= 0) {
    browser = "Firefox";
   } 
   else if(browserAgent != null && browserAgent.indexOf("Safari") >= 0){
    browser = "Safari";
   }    
   else if(browserAgent != null && browserAgent.indexOf("MSIE") >= 0){
    browser = "IE";
   }   
   else if(browserAgent != null && browserAgent.indexOf("Opera") >= 0){
    browser = "Opera";
   }
   else {
    browser = "Undefined";
   }
   return browser;
  }
 }
}

 

在as2的时代,只有onRelease下的getURL才会被认为是一次完整的点击,onPress就会被拦截

到了navigateToURL的时代,即便你监听一个MouseEvent.CLICK事件,他也会被拦截,原因我现在也不知道,谁知道的话可以跟帖

那么怎么解决as3弹出一个_blank不被拦截呢,3个条件

1.判断浏览器的版本,如果是firefox和其他什么opera啥的就直接navigateToURL,如果是IE的话就调用window.open
2.必须监听MouseEvent.CLICK事件,如果是MouseEvent.DOWN啥的我就不敢保证了
3.wmode属性必须设置成opaque,Transparent的话我没试过,大家可以试试,反正window肯定不行

这3个条件满足了,才能保证弹出的新页面不被拦截!!!!!!!!!!