AS:Flash AS3中获取浏览器信息及URL相关参数(并非swf url地址)
好久没来这里了,最近发现网络上对此类信息的封装少的可怜,没有一个是比较完整的,今天又是周未,不敲点代码手痒痒的,^_^,所以本人手贱借此时发布一篇是关于 AS3中获取浏览器信息及URL相关参数的WebUtils类,欢迎大家拍砖以使此类更丰富一些!
先来看看效果,因本机没装多个浏览器测试,所以只截了几个浏览器的获取的结果
IE:
360,内核是IE核心,所以效果跟IE基本一样
FF,即FireFox(火狐中国版)
以下是Test.as测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package { import flash.events.*; import flash.display.*; import fl.controls.Button; import com.VvxT.WebUtils; public class Test extends MovieClip { public function Test() { txt0.appendText(WebUtils.BrowserAgent); txt1.appendText( "是否是IE:" +WebUtils.IsIE.toString()+ "\r\n" ); txt1.appendText( "是否是FireFox:" +WebUtils.IsMozilla.toString()+ "\r\n" ); txt1.appendText( "是否是Safari:" +WebUtils.IsSafari.toString()+ "\r\n" ); txt1.appendText( "是否是Opera:" +WebUtils.IsOpera.toString()+ "\r\n" ); txt1.appendText( "浏览器类型:" +WebUtils.BrowserMatch().browser+ " 浏览器版本:" +WebUtils.BrowserMatch().version + "\r\n" ); txt2.appendText( "获取URL地址:" +WebUtils.Url + "\r\n" ); txt2.appendText( "获取所有URL参数:" + WebUtils.QueryString + "\r\n" ); txt2.appendText( "获取URL参数id=:" + WebUtils.Request( "id" ) + "\r\n" ); txt2.appendText( "获取锚点参数:" + WebUtils.Hash + "\r\n" ); txt2.appendText( "获取主机头:" + WebUtils.Host + "\r\n" ); txt2.appendText( "获取访问协议:" + WebUtils.Protocol + "\r\n" ); txt2.appendText( "获取访问端口:" + WebUtils.Port + "\r\n" ); txt2.appendText( "获取页面名称路径:" + WebUtils.PathAndName + "\r\n" ); txt2.appendText( "页面编码:" + WebUtils.PageEncoding + "\r\n" ); jsAlert.addEventListener(MouseEvent.CLICK,btnAlertClick); jsOpen.addEventListener(MouseEvent.CLICK,btnOpenClick); jsUrl.addEventListener(MouseEvent.CLICK,btnUrlClick); } private function btnAlertClick(evt:MouseEvent): void { WebUtils.Alert( '弹出JS对话框示例!' ); } private function btnOpenClick(evt:MouseEvent): void { WebUtils.Eval( "javascript:window.open('http://www.baidu.com','newwindow')" ); } private function btnUrlClick(evt:MouseEvent): void { WebUtils.Redirect( "Test.html?id=" +Math.ceil(Math.random()*999999)+ "&cid=ok&page=" +Math.ceil(Math.random()*10)+ "#end" ); } } } |
以下是WebUtils.as类(类包文件夹结构:com\VvxT\WebUtils.as):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | package com.VvxT { import flash.net.*; import flash.utils.ByteArray; import flash.external.ExternalInterface; /** * 统一资源定位符 (Uniform Resource Locator, URL) 完整的URL由这几个部分构成: * scheme://host:port/path?query#fragment * PS:所有获取失败时返回null或"" */ public class WebUtils { private static var regWebkit:RegExp = new RegExp( "(webkit)[ \\/]([\\w.]+)" , "i" ); /** * 整个URl字符串 EX.:返回值:http://www.test.com:80/view.html?id=123#start */ public static function get Url():String { return getUrlParams( "url" ); } /** * 整个URl字符串 EX.:返回值:http://www.test.com:80/view.html?id=123#start */ public static function get Href():String { return getUrlParams( "href" ); } /** * 获取URL中的锚点(信息片断) EX.:返回值:#start */ public static function get Hash():String { return getUrlParams( "hash" ); } /** * URL 的端口部分。如果采用默认的80端口(PS:即使手动添加了:80),那么返回值并不是默认的80而是空字符。 */ public static function get Port():String { return (getUrlParams( "port" ) ? getUrlParams( "port" ) : "80" ); } /** * URL 的路径部分(就是文件地址) EX.:返回值:/view.html */ public static function get PathAndName():String { return getUrlParams( "PathAndName" ); } /** * URL 的路径部分(就是文件地址) EX.:返回值:/view.html */ public static function get Pathname():String { return getUrlParams( "pathname" ); } /** * 查询(参数)部分。除了给动态语言赋值以外的参数 EX.:返回值:?id=123 */ public static function get Search():String { return getUrlParams( "search" ); } /** * 查询(参数)部分。除了给动态语言赋值以外的参数 EX.:返回值:?id=123 */ public static function get QueryString():String { return getUrlParams( "query" ); } /** * URL 的协议部分 EX.:返回值:http:、https:、ftp:、maito:等 */ public static function get Protocol():String { return getUrlParams( "protocol" ); } /** * URL 的主机部分,EX.:返回值:www.test.com */ public static function get Host():String { return getUrlParams( "host" ); } public static function Request(param:String):String { var returnValue:String; try { var query:String = QueryString.substr(1); var urlv:URLVariables= new URLVariables(); urlv.decode(query); returnValue = urlv[param]; } catch (error:Object) { } if (returnValue == null ) { returnValue = "" ; } return returnValue; } private static function getUrlParams(param:String):String { var returnValue:String; switch (param) { case "PathAndName" : returnValue = ExternalInterface.call( "function getUrlParams(){return window.location.pathname;}" ); break ; case "query" : returnValue = ExternalInterface.call( "function getUrlParams(){return window.location.search;}" ); break ; case "url" : returnValue = ExternalInterface.call( "function getUrlParams(){return window.location.href;}" ); break ; default : returnValue = ExternalInterface.call( "function getUrlParams(){return window.location." + param + ";}" ); break ; } return (returnValue ? UrlDecode(returnValue): "" ); } /** * 获取浏览器信息 */ public static function get BrowserAgent():String { var returnValue:String = ExternalInterface.call( "function BrowserAgent(){return navigator.userAgent;}" ); return (returnValue ? returnValue: "" ); } /** * 是否IE浏览器 */ public static function get IsIE():Boolean { return (BrowserMatch().browser.toLowerCase() == "msie" ); } /** * 是否FireFox浏览器 */ public static function get IsMozilla():Boolean { return (BrowserMatch().browser.toLowerCase() == "mozilla" ); } /** * 是否Safari浏览器 */ public static function get IsSafari():Boolean { return regWebkit.test(BrowserAgent); } /** * 是否Opera浏览器 */ public static function get IsOpera():Boolean { return (BrowserMatch().browser.toLowerCase() == "opera" ); } /** * 获取浏览器类型及对应的版本信息 EX.:BrowserMatch().browser BrowserMatch().version */ public static function BrowserMatch():Object { var ua:String = BrowserAgent; var ropera:RegExp = new RegExp( "(opera)(?:.*version)?[ \\/]([\\w.]+)" , "i" ); var rmsie:RegExp = new RegExp( "(msie) ([\\w.]+)" , "i" ); var rmozilla:RegExp = new RegExp( "(mozilla)(?:.*? rv:([\\w.]+))?" , "i" ); var match:Object = regWebkit.exec(ua) || ropera.exec(ua) || rmsie.exec(ua) || ua.indexOf( "compatible" ) < 0 && rmozilla.exec(ua) || []; return { browser: match[1] || "" , version: match[2] || "0" }; } /** * 获取页面编码方式,EX.:返回值:GB2312、UTF-8等; */ public static function get PageEncoding():String { var returnValue:String = ExternalInterface.call( "function PageEncoding(){return window.document.charset;}" ); //IE if (returnValue == null ) { returnValue = ExternalInterface.call( "function PageEncoding(){return window.document.characterSet;}" ); } //FF //获取成功 if (returnValue != null ) { returnValue = returnValue.toUpperCase(); } return (returnValue ? returnValue: "" ); } /** * 通过js弹出浏览器提示alert,EX.:Alert("Test"); */ public static function Alert(msg:String): void { navigateToURL( new URLRequest( "javascript:alert('" +msg+ "');" ), "_self" ); } /** * 通过js的open新窗口打开,(PS:多标签浏览器则新建一个标签打开) */ public static function Open(url:String): void { Eval( "javascript:window.open('" +url+ "','newwindow')" ); } /** * URL重定向,使用replace函数,(PS:取消浏览器的前进后退,防止刷新回发数据) */ public static function Redirect(url:String): void { Eval( "window.location.replace('" +url + "')" ); } /** * URL重定向,使用内部navigateToURL函数,(PS:简化了不用每次都new URLRequest的操作) */ public static function NavigateToURL(url:String,target:String= "_self" ): void { navigateToURL( new URLRequest(url), target); } /** * 运行js语句,eval */ public static function Eval(code:String):Object { var rtn:Object = ExternalInterface.call( "eval" ,code + ";void(0);" ); return rtn; } /** * URL编码,encoding为空时应用统一的UTF-8编码处理,可设"GB2312"、"UTF-8"等,(兼容性处理,对应JS中的escape) */ public static function UrlEncode(str:String,encoding:String = "" ):String { if (str == null || str == "" ) { return "" ; } if (encoding == null || encoding == "" ) { return encodeURI(str); } var returnValue:String = "" ; var byte :ByteArray = new ByteArray(); byte .writeMultiByte(str,encoding); for ( var i: int ; i< byte .length; i++) { returnValue += escape(String.fromCharCode( byte [i])); } return returnValue; } /** * URL解码,encoding为空时应用统一的UTF-8编码处理,可设"GB2312"、"UTF-8"等,(兼容性处理,对应JS中的unescape) */ public static function UrlDecode(str:String,encoding:String = "" ):String { if (str == null || str == "" ) { return "" ; } if (encoding == null || encoding == "" ) { return decodeURI(str); } var returnValue:String = "" ; var byte :ByteArray = new ByteArray(); byte .writeMultiByte(str,encoding); for ( var i: int ; i< byte .length; i++) { returnValue += unescape(String.fromCharCode( byte [i])); } return returnValue; } } } |
以下是所有文件的打包,方便读者测试:
https://files.cnblogs.com/VvxT/WebUtils.rar
PS:以上均为原创,欢迎讨论拍砖!
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
汗,研究了一会,没有看到有Flash或AS3类的分类,不知发布到哪里了,随便选了一个Flex,哈哈
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)