Flex读取浏览器参数

这个类提供的功能有:

  1. 从浏览器中获取完整的URL
  2. 主机名
  3. 当前所使用的协议
  4. 端口
  5. 主域名后面的路径
  6. 参数值

原文地址:http://www.flexpasta.com/index.php/2008/03/15/getting-url-parameters-host-name-port-in-flex-actionscript/

package com.flexpasta.utils { import flash.external.ExternalInterface; import mx.core.Application; import mx.collections.ArrayCollection; import mx.controls.Alert; public class HTTPUtil { //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * @private */ public function HTTPUtil() { super(); } /** * Returns the current url that the swf player lives in * */ public static function getUrl():String { return ExternalInterface.call("window.location.href.toString"); } /** * Returns the current host name. * example: http://www.flexpasta.com/?x=1&y=2 would return www.flexpasta.com */ public static function getHostName():String { return ExternalInterface.call("window.location.hostname.toString"); } /** * Returns the current protocol, such as http:, https:, etc * */ public static function getProtocol():String { return ExternalInterface.call("window.location.protocol.toString"); } /** * Gets the current port for the url */ public static function getPort():String { return ExternalInterface.call("window.location.port.toString"); } /** * Gets the context following the base of the url * Example http://www.flexpasta.com/test?x=1&y=2 would return /test */ public static function getContext():String { return ExternalInterface.call("window.location.pathname.toString"); } /** * Gets a url parameter value by name and returns it as a String. This function works without flash vars being passed into the swf. * For example, even if the url is something like, domain.com?MyFlexPage.html?b=5, it will still return 5 when 'b' is passed. * The function uses javascript to find the url paramters. Please note however that this will only work for a GET request and not a POST. */ public static function getParameterValue(key:String):String { var value:String; var uparam:String = ExternalInterface.call("window.location.search.toString"); if(uparam==null) { return null; } var paramArray:ArrayCollection = new ArrayCollection(uparam.split('&')); for(var x:int=0; x<paramArray.length; x++) { var p:String = paramArray.getItemAt(x) as String; if(p.indexOf(key + '=')>-1) { value = (p.replace((key + '='), '')).replace('?',''); x=paramArray.length; } } return value; } } }

PS:原文中有很多评论,有兴趣可以看一看,Flex 的mx.utils.URLUtil 也提供了很多有用的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值