as3和js分别判断用户终端的浏览器类型的方法实例
as3和js分别判断用户终端的浏览器类型的方法实例JS的写法[*]function getBrowser(){
[*]
[*] var useragent = navigator.userAgent.toLowerCase();
[*]
[*] if(/(.*)maxthon(.*)/.test(useragent)){
[*] document.write('遨游');
[*] }
[*] else if(/(.*)firefox(.*)/.test(useragent)){
[*] document.write("火狐");
[*] }
[*] else if(/(.*)chrome(.*)/.test(useragent)){
[*] document.write("谷歌");
[*] }
[*] else if(/(.*)msie(.*)/.test(useragent)){
[*] document.write("IE");
[*] }
[*] else if(/(.*)mozilla(.*)/.test(useragent) && !/(.*)firefox(.*)/.test(useragent) && !/(.*)chrome(.*)/.test(useragent) && !/(.*)msie(.*)/.test(useragent)){
[*] document.write("safari");
[*] }else{
[*] document.write("cuplayer.com未知");
[*] }
[*]
[*] return useragent;
[*]
[*] }
AS的写法
[*]package com.ylsoft.lib{
[*] import flash.external.ExternalInterface;
[*] import flash.net.URLRequest;
[*] import flash.net.navigateToURL;
[*]
[*] public class WebWindow {
[*] 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() == "maxthon"){
[*] ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
[*] //navigateToURL(myURL, window);
[*] }
[*] //If IE,
[*] else if(browserName == "firefox"){
[*] navigateToURL(myURL, window);
[*] }
[*] //If Safari
[*] else if(browserName == "chrome"){
[*] navigateToURL(myURL, window);
[*] }
[*] //If Opera
[*] else if(browserName == "msie"){
[*] ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
[*] }
[*] else if(browserName == "safari"){
[*] ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
[*] }
[*] 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.toLowerCase();}");
[*]
[*] // Debug.text += "Browser Info: [" + browserAgent + "]";
[*]
[*] //Determines brand of browser using a find index. If not found indexOf returns (-1).
[*] if(browserAgent != null && /(.*)maxthon(.*)/.test(browserAgent)) {
[*] browser = "maxthon";
[*] }
[*] else if(browserAgent != null && /(.*)firefox(.*)/.test(browserAgent)){
[*] browser = "firefox";
[*] }
[*] else if(browserAgent != null && /(.*)chrome(.*)/.test(browserAgent)){
[*] browser = "chrome";
[*] }
[*] else if(browserAgent != null && /(.*)msie(.*)/.test(browserAgent)){
[*] browser = "msie";
[*] }
[*] else if(browserAgent != null && /(.*)mozilla(.*)/.test(browserAgent) && !/(.*)firefox(.*)/.test(browserAgent) && !/(.*)chrome(.*)/.test(browserAgent) && !/(.*)msie(.*)/.test(browserAgent)){
[*] browser = "safari";
[*] }
[*]
[*] return browser;
[*] }
[*] }
[*]}
页:
[1]