許多行動裝置不支援 Flash,需在載入頁面時判斷裝置類型,決定載入 .swf (PC) 或 .gif (行動裝置) 等替代內容。
function isMobileUA() {
var ua = navigator.userAgent.toLowerCase();
var mobileKeywords = [
'mobile', 'android', 'iphone', 'ipad', 'ipod',
'blackberry', 'windows phone', 'opera mini', 'iemobile'
];
return mobileKeywords.some(function(kw) { return ua.indexOf(kw) > -1; });
}
// 使用
if (isMobileUA()) {
// 載入 GIF/HTML5 動畫
} else {
// 載入 Flash SWF
}
function isTouchDevice() {
return ('ontouchstart' in window) ||
(navigator.maxTouchPoints > 0) ||
(navigator.msMaxTouchPoints > 0); // IE10/11
}
// 更精確:判斷「主要輸入裝置為觸控」
function isPrimaryTouch() {
return window.matchMedia('(pointer:coarse)').matches;
}
/**
* 綜合檢測:Flash 支援度 + 裝置類型
* @returns {Object} { hasFlash: Boolean, flashVersion: Number, isMobile: Boolean }
*/
function detectFlashAndDevice() {
var hasFlash = false;
var flashVersion = 0;
var isMobile = isMobileUA() || isTouchDevice(); // 綜合判斷
// Flash 檢測 (僅桌面瀏覽器相關)
if (!isMobile) {
if (/*@cc_on!@*/false) { // IE 條件編譯
try {
var swf = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
if (swf) {
hasFlash = true;
var verStr = swf.GetVariable('$version'); // "WIN 32,0,0,100"
flashVersion = parseInt(verStr.split(' ')[1].split(',')[0], 10);
}
} catch (e) {}
} else if (navigator.plugins && navigator.plugins['Shockwave Flash']) {
var plugin = navigator.plugins['Shockwave Flash'];
hasFlash = true;
var desc = plugin.description; // "Shockwave Flash 32.0 r0"
flashVersion = parseInt(desc.match(/\d+/)[0], 10);
}
}
return { hasFlash: hasFlash, flashVersion: flashVersion, isMobile: isMobile };
}
// 使用範例
var detect = detectFlashAndDevice();
if (detect.isMobile || !detect.hasFlash) {
// 載入替代內容 (GIF/HTML5/靜態圖)
renderFallback();
} else {
// 載入 Flash
renderFlash(detect.flashVersion);
}
Adobe Flash 已於 2020/12/31 終止支援**,所有主流瀏覽器已移除 Flash Player。新專案**嚴禁使用 Flash**,現有專案應全面遷移:
| 原 Flash 用途 | 現代替代技術 |
|---|---|
| 影片播放 | <video> + H.264/VP9/WebM、HLS/DASH 串流 |
| 動畫/互動 | Canvas API、WebGL、SVG SMIL、CSS Animation、Lottie (Bodymovin) |
| 遊戲 | WebGL (Three.js, Babylon.js)、WebAssembly |
| 豐富文字編輯 | contenteditable + ExecCommand / Input Events API / Slate/ProseMirror |
| 檔案上傳/攝影機 | <input type="file" accept="image/*" capture>, MediaDevices API |