DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
讓嵌入的 Flash 通過標準驗證:一個 JS 間接寫法
編輯:CSS詳解     

常有網友問:網頁裡嵌入的 Flash 標籤該怎麼寫才符合 Web 標準?老實說,目前沒有一個真正完美的解法。本文示範一種常見的變通方式——把 object/embed 這段標籤寫進 JS 檔,透過函式參數傳入檔案路徑與尺寸,讓頁面本身不必出現不合標準的標籤。

先建立 Flash.js

function swf(file,w,h) {
   document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/swflash.cab#version=7,0,19,0" width="'+w+'" height="'+h+'">');
   document.write('<param name="movie" value="' + file + '">');
   document.write('<param name="quality" value="high">');
   document.write('<param name="wmode" value="transparent">');
   document.write('<param name="menu" value="false">');
   document.write('<embed src="' + file + '" quality="high" pluginspage="https://www.macromedia.com/go/getFlashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>');
   document.write('</object>');
}

這個函式定義了三個參數:file 是 Flash 檔路徑,w 與 h 分別是寬與高。在 XHTML 中呼叫它並傳入參數,就能完成嵌入。

在頁面中呼叫

<div id="Flash">
  <script type="text/javascript">swf('webjxcom.swf','500','220');</script>
</div>

這裡用一個 id 為 Flash 的 div 當容器,內部放 script 呼叫,參數依序為檔案路徑、寬度、高度。

完整範例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>符合標準的 Flash 嵌入方式</title>
<script type="text/javascript" src="Flash.js"></script>
<style type="text/css">
#Flash { width:500px; margin:50px auto; border:5px solid #03c;}
</style>
</head>
<body>
<div id="Flash">
  <script type="text/javascript">swf('webjxcom.swf','500','220');</script>
</div>
</body>
</html>

必須說清楚的限制

這是一個換湯不換藥的做法:不合標準的標籤並沒有消失,只是被搬進了 JS 字串裡,驗證器看不到而已。通過驗證只是一種表象,這樣的思路是否可取,請依專案實際情況自行斟酌。

更值得留意的副作用是:使用 document.write 輸出標籤,代表完全依賴 JavaScript。使用者停用 JS、或搜尋引擎的爬蟲不執行這段程式時,內容就不會存在;此外,Flash 本身缺乏無障礙替代內容,行動裝置也早已全面不支援。若專案仍在維護既有 Flash 內容,建議至少加上 <noscript> 提供替代連結或說明。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved