Flash超链接 - 更改网址中的var名称

时间:2012-12-14 11:56:14

标签: actionscript-3 flash hyperlink

我在网站上的flash中遇到了超链接。由于网站在CMS中,因此不同 批准的阶段我不知道确切的网址,所以

function piClick(e:MouseEvent):void
{
  var url:String = "/cms__Main?name=Target_name";
  var request:URLRequest = new URLRequest(url);
  try {
    navigateToURL(request, '_self');
  } catch (e:Error) {
    trace("Error occurred!");
  }
} 

不起作用,因为cms_Main根据网站的阶段而变化。我可能需要做的是:

  • 抓住网址(或者如果可能的话,在最后一个“/”之后部分)
  • 在String
  • 中更改“name”变量

f.e

https://domain_name/.../status?name=Name_I_need_to_swap&sname=Country_name&..

2 个答案:

答案 0 :(得分:0)

您可以使用this.loaderInfo.url

获取网址

答案 1 :(得分:0)

stage.loaderInfo.url会获取swf本身的地址,因此它不会为您提供完整的地址,查询或哈希标记。但是,您可以使用Javascript获取所有这些信息。

import flash.external.ExternalInterface;

function getFullAddress():String {
    if (ExternalInterface.available) {
        var href:String = ExternalInterface.call('function(){return window.location.href}');
        if (href) return href.toString();
    }
    throw new Error('Make sure JS is enabled.');
}
var fullAddr:String = getFullAddress();

编辑:以下是您可以获取和修改所有问题的方法。

import flash.external.ExternalInterface;
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.navigateToURL;


function getFullAddress():String {
    if (ExternalInterface.available) {
        var href:String = ExternalInterface.call('function(){return window.location.href}');
        if (href) return href.toString();
    }
    throw new Error('Make sure JS is enabled.');
}
function getQuery(fullAddr:String):URLVariables {
    if (!fullAddr.match(/\?[^#]/)) return new URLVariables();
    var q:String = fullAddr.replace(/^.*?\?([^#]+)(#.*)?$/,'$1');
    return new URLVariables(q);
}
function getAddress(fullAddr:String):String {
    return fullAddr.replace(/\?.*/,'').replace(/#.*/,'');
}
function getRequest(url:String,query:URLVariables=null,method:String='GET'):URLRequest {
    method = URLRequestMethod[method] || 'GET';
    var req:URLRequest = new URLRequest(url);
    req.method = method;
    if (method == 'GET' && query != null) {
        req.url += '?' + query.toString().replace(/%5f/gi,'_').replace(/%2d/gi,'-');
        // this is because dash and underscore chars are also
        // encoded by URLVariables. we want to omit this.
        return req;
    }
    req.data = query;
    return req;
}


var fullAddr:String = getFullAddress();
var addr:String = getAddress(fullAddr);

var query:URLVariables = getQuery(fullAddr);
query.name = 'Name_I_need_to_swap';
query.sname = 'Country_name';
// add as many variable-value pairs as you like
// and don't worry, they will be automatically
// encoded by the function called below

var req:URLRequest = getRequest(addr,query);
//navigateToURL(req);

// check the console to see the changed address
ExternalInterface.call('console.log',req);