·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3和js分别判断用户终端的浏览器类型的方法实例

时间:2015-01-07 22:43酷播
[AS3]as3和js分别判断用户终端的浏览器类型的方法实例

[AS3]as3和js分别判断用户终端的浏览器类型的方法实例

JS的写法

  1. function getBrowser(){ 
  2.     
  3.       var useragent = navigator.userAgent.toLowerCase(); 
  4.  
  5.        if(/(.*)maxthon(.*)/.test(useragent)){ 
  6.            document.write('遨游'); 
  7.        } 
  8.        else if(/(.*)firefox(.*)/.test(useragent)){ 
  9.            document.write("火狐"); 
  10.        } 
  11.        else if(/(.*)chrome(.*)/.test(useragent)){ 
  12.            document.write("谷歌"); 
  13.        } 
  14.        else if(/(.*)msie(.*)/.test(useragent)){ 
  15.            document.write("IE"); 
  16.        } 
  17.        else if(/(.*)mozilla(.*)/.test(useragent) && !/(.*)firefox(.*)/.test(useragent) && !/(.*)chrome(.*)/.test(useragent) && !/(.*)msie(.*)/.test(useragent)){ 
  18.            document.write("safari"); 
  19.        }else{ 
  20.             document.write("cuplayer.com未知"); 
  21.        } 
  22.         
  23.        return useragent; 
  24.     
  25.    } 

AS的写法

  1. package com.ylsoft.lib{ 
  2.     import flash.external.ExternalInterface; 
  3.     import flash.net.URLRequest; 
  4.     import flash.net.navigateToURL; 
  5.      
  6.     public class WebWindow { 
  7.         public function openWindow(url:String, window:String="_blank"features:String=""){ 
  8.              
  9.             var WINDOW_OPEN_FUNCTION:String = "window.open"
  10.             var myURL:URLRequest = new URLRequest(url);    
  11.             var browserName:String = getBrowserName(); 
  12.              
  13.             if(getBrowserName() == "maxthon"){ 
  14.                 ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features); 
  15.                 //navigateToURL(myURL, window); 
  16.             } 
  17.                 //If IE,  
  18.             else if(browserName == "firefox"){ 
  19.                 navigateToURL(myURL, window); 
  20.             } 
  21.                 //If Safari  
  22.             else if(browserName == "chrome"){      
  23.                 navigateToURL(myURL, window); 
  24.             } 
  25.                 //If Opera  
  26.             else if(browserName == "msie"){  
  27.                 ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features); 
  28.             }  
  29.             else if(browserName == "safari"){ 
  30.                 ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features); 
  31.             } 
  32.             else { 
  33.                 navigateToURL(myURL, window); 
  34.             } 
  35.              
  36.             /*Alternate methodology... 
  37.             var popSuccess:Boolean = ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features); 
  38.             if(popSuccess == false){ 
  39.             navigateToURL(myURL, window); 
  40.             }*/ 
  41.              
  42.         } 
  43.         private function getBrowserName():String{ 
  44.             var browser:String; 
  45.              
  46.             //Uses external interface to reach out to browser and grab browser useragent info. 
  47.             var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent.toLowerCase();}"); 
  48.              
  49.             // Debug.text += "Browser Info: [" + browserAgent + "]"; 
  50.              
  51.             //Determines brand of browser using a find index. If not found indexOf returns (-1). 
  52.             if(browserAgent != null && /(.*)maxthon(.*)/.test(browserAgent)) { 
  53.                 browser = "maxthon"
  54.             }  
  55.             else if(browserAgent != null && /(.*)firefox(.*)/.test(browserAgent)){ 
  56.                 browser = "firefox"
  57.             }     
  58.             else if(browserAgent != null && /(.*)chrome(.*)/.test(browserAgent)){ 
  59.                 browser = "chrome"
  60.             }    
  61.             else if(browserAgent != null && /(.*)msie(.*)/.test(browserAgent)){ 
  62.                 browser = "msie"
  63.             } 
  64.             else if(browserAgent != null && /(.*)mozilla(.*)/.test(browserAgent) && !/(.*)firefox(.*)/.test(browserAgent) && !/(.*)chrome(.*)/.test(browserAgent) && !/(.*)msie(.*)/.test(browserAgent)){ 
  65.                 browser = "safari"
  66.             } 
  67.              
  68.             return browser; 
  69.         } 
  70.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器