AS 3.0與各瀏覽器相互數據調用

因各家browser之間存在差異,開發者首先獲取當前browser類型,再調用相應的接口程序。

/**

* return current browser name.
*/
private static function getBrowserName():String
{
var browser:String;

//Uses external interface to reach out to browser and grab browser useragent info.
var browserAgent:String = "";
if (ExternalInterface.available)
{
browserAgent = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
}
//Determines brand of browser using a find index. If not found indexOf returns (-1).
if (browserAgent != null && browserAgent.indexOf("Firefox") >= 0)
{
browser = "Firefox";
}
else if (browserAgent != null && browserAgent.indexOf("Safari") >= 0)
{
browser = "Safari";
}
else if (browserAgent == null || browserAgent.indexOf("MSIE") >= 0)
{
browser = "IE";
}
else if (browserAgent != null && browserAgent.indexOf("Opera") >= 0)
{
browser = "Opera";
}
else
{
browser = "Undefined";
}
return (browser);

}

打開一個瀏覽器窗口實例:

               protected static const WINDOW_OPEN_FUNCTION:String = "window.open";

/**
* Open a new browser window and prevent browser from blocking it.
*
* @param url        url to be opened
* @param window     window target
* @param features   additional features for window.open function
*/
public static function openWindow(url:String, window:String = "_blank", features:String = ""):void
{
var browserName:String = getBrowserName();

if (browserName == "Firefox")
{
ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
}
//If IE, 
else if (browserName == "IE")
{
ExternalInterface.call("function setWMWindow() {window.open('" + url + "');}");
}
//If Safari 
else if (browserName == "Safari")
{
navigateToURL(new URLRequest(url), window);
}
//If Opera 
else if (browserName == "Opera")
{
navigateToURL(new URLRequest(url), window);
}
//Otherwise, use Flash's native 'navigateToURL()' function to pop-window. 
//This is necessary because Safari 3 no longer works with the above ExternalInterface work-a-round.
else
{
navigateToURL(new URLRequest(url), window);
}
}

發佈了42 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章