不知大家注意到没有,鼠标放在网页的Flash上时,会出现个虚线框,并且有时还会显示'单击以激活并使用此控件'。这样给人感觉不是很好,不好,我们就要想办法去掉它。我整理了一下几个用Javascript实现此功能的方法,大家有兴趣可以看一下:
1.在页面中引入下面这段代码,页面上所有Flash在浏览时都不会在有虚线框:
1/**//*
2 xpengfee search from www at 2007-08-24
3 put it in your page,then the flash of the page will be no deshed
4*/
5var ie = (document.defaultCharset && document.getElementById && !window.home);
6var opera9 = false;
7if (ie)
8{
9 //Check for ie 5.5 and exclude it from the script
10 var ver=navigator.appVersion.split("MSIE")
11 ver=parseFloat(ver[1])
12 ie = (ver >=6)
13}
14else if (navigator.userAgent.indexOf("Opera")!=-1)
15{
16 //Check for Opera9 and include it in the ObjectSwap
17 var versionindex=navigator.userAgent.indexOf("Opera")+6
18 if (parseInt(navigator.userAgent.charAt(versionindex))>=9)
19 opera9 = true;
20}
21//Perform ObjectSwap if the browser is IE or Opera (if not just check flashVersion)
22var oswap = (ie || opera9)
23
24//Hide the object to prevent it from loading twice
25if (oswap)
26{
27 document.write ("<style id='hideObject'> object{display:none;} </style>");
28}
29
30/**//*Replace all flash objects on the page with the same flash object,
31by rewriting the outerHTML values
32This bypasses the new IE ActiveX object activation issue*/
33objectSwap = function()
34{
35 if (!document.getElementsByTagName)
36 {
37 return;
38 }
39 //An array of ids for flash detection
40 var stripQueue = [];
41 //Get a list of all ActiveX objects
42 var objects = document.getElementsByTagName('object');
43 for (var i=0; i<objects.length; i++)
44 {
45 var o = objects[i];
46 var h = o.outerHTML;
47 //The outer html omits the param tags, so we must retrieve and insert these separately
48 var params = "";
49 var hasFlash = true;
50 for (var j = 0; j<o.childNodes.length; j++)
51 {
52 var p = o.childNodes[j];
53 if (p.tagName == "PARAM")
54 {
55 //Check for version first - applies to all browsers
56 //For this to work, a new param needs to be included in the object with the name "flashVersion" eg:
57 //<param name="flashVersion" value="7" />
58 if (p.name == "flashVersion")
59 {
60 hasFlash = detectFlash(p.value);
61 if (!hasFlash)
62 {
63 //Add the objects id to the list (create a new id if there's isn't one already)
64 o.id = (o.id == "") ? ("stripFlash"+i) : o.id;
65 stripQueue.push(o.id);
66 break;
67 }
68 }
69 params += p.outerHTML;
70 }
71 }
72 if (!hasFlash)
73 {
74 continue;
75 }
76 //Only target internet explorer
77 if (!oswap)
78 {
79 continue;
80 }
81 //Avoid specified objects, marked with a "noswap" classname
82 if (o.className.toLowerCase().indexOf ("noswap") != -1)
83 {
84 continue;
85 }
86 //Get the tag and attributes part of the outer html of the object
87 var tag = h.split(">")[0] + ">";
88 //Add up the various bits that comprise the object:
89 //The tag with the attributes, the params and it's inner html
90 var newObject = tag + params + o.innerHTML + " </OBJECT>";
91 //And rewrite the outer html of the tag
92 o.outerHTML = newObject;
93 }
94 //Strip flash objects
95 if (stripQueue.length)
96 {
97 stripFlash(stripQueue)
98 }
99 //Make the objects visible again
100 if (oswap)
101 {
102 document.getElementById("hideObject").disabled = true;
103 }
104}
105
106detectFlash = function(version)
107{
108 if(navigator.plugins && navigator.plugins.length)
109 {
110 //Non-IE flash detection.
111 var plugin = navigator.plugins["Shockwave Flash"];
112 if (plugin == undefined)
113 {
114 return false;
115 }
116 var ver = navigator.plugins["Shockwave Flash"].description.split(" ")[2];
117 return (Number(ver) >= Number(version))
118 } else if (ie && typeof (ActiveXObject) == "function")
119 {
120 //IE flash detection.
121 try
122 {
123 var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + version);
124 return true;
125 }
126 catch(e)
127 {
128 return false;
129 }
130 }
131 //Catchall - skip detection
132 return true;
133}
134
135//Loop through an array of ids to strip
136//Replace the object by a div tag containing the same innerHTML.
137//To display an alternative image, message for the user or a link to the flash installation page, place it inside the object tag.
138//For the usual object/embed pairs it needs to be enclosed in comments to hide from gecko based browsers.
139stripFlash = function (stripQueue)
140{
141 if (!document.createElement)
142 {
143 return;
144 }
145 for (var i=0; i<stripQueue.length; i++)
146 {
147 var o = document.getElementById(stripQueue[i]);
148 var newHTML = o.innerHTML;
149 //Strip the comments
150 newHTML = newHTML.replace(/<!--\s/g, "");
151 newHTML =
1.在页面中引入下面这段代码,页面上所有Flash在浏览时都不会在有虚线框:
1/**//*
2 xpengfee search from www at 2007-08-24
3 put it in your page,then the flash of the page will be no deshed
4*/
5var ie = (document.defaultCharset && document.getElementById && !window.home);
6var opera9 = false;
7if (ie)
8{
9 //Check for ie 5.5 and exclude it from the script
10 var ver=navigator.appVersion.split("MSIE")
11 ver=parseFloat(ver[1])
12 ie = (ver >=6)
13}
14else if (navigator.userAgent.indexOf("Opera")!=-1)
15{
16 //Check for Opera9 and include it in the ObjectSwap
17 var versionindex=navigator.userAgent.indexOf("Opera")+6
18 if (parseInt(navigator.userAgent.charAt(versionindex))>=9)
19 opera9 = true;
20}
21//Perform ObjectSwap if the browser is IE or Opera (if not just check flashVersion)
22var oswap = (ie || opera9)
23
24//Hide the object to prevent it from loading twice
25if (oswap)
26{
27 document.write ("<style id='hideObject'> object{display:none;} </style>");
28}
29
30/**//*Replace all flash objects on the page with the same flash object,
31by rewriting the outerHTML values
32This bypasses the new IE ActiveX object activation issue*/
33objectSwap = function()
34{
35 if (!document.getElementsByTagName)
36 {
37 return;
38 }
39 //An array of ids for flash detection
40 var stripQueue = [];
41 //Get a list of all ActiveX objects
42 var objects = document.getElementsByTagName('object');
43 for (var i=0; i<objects.length; i++)
44 {
45 var o = objects[i];
46 var h = o.outerHTML;
47 //The outer html omits the param tags, so we must retrieve and insert these separately
48 var params = "";
49 var hasFlash = true;
50 for (var j = 0; j<o.childNodes.length; j++)
51 {
52 var p = o.childNodes[j];
53 if (p.tagName == "PARAM")
54 {
55 //Check for version first - applies to all browsers
56 //For this to work, a new param needs to be included in the object with the name "flashVersion" eg:
57 //<param name="flashVersion" value="7" />
58 if (p.name == "flashVersion")
59 {
60 hasFlash = detectFlash(p.value);
61 if (!hasFlash)
62 {
63 //Add the objects id to the list (create a new id if there's isn't one already)
64 o.id = (o.id == "") ? ("stripFlash"+i) : o.id;
65 stripQueue.push(o.id);
66 break;
67 }
68 }
69 params += p.outerHTML;
70 }
71 }
72 if (!hasFlash)
73 {
74 continue;
75 }
76 //Only target internet explorer
77 if (!oswap)
78 {
79 continue;
80 }
81 //Avoid specified objects, marked with a "noswap" classname
82 if (o.className.toLowerCase().indexOf ("noswap") != -1)
83 {
84 continue;
85 }
86 //Get the tag and attributes part of the outer html of the object
87 var tag = h.split(">")[0] + ">";
88 //Add up the various bits that comprise the object:
89 //The tag with the attributes, the params and it's inner html
90 var newObject = tag + params + o.innerHTML + " </OBJECT>";
91 //And rewrite the outer html of the tag
92 o.outerHTML = newObject;
93 }
94 //Strip flash objects
95 if (stripQueue.length)
96 {
97 stripFlash(stripQueue)
98 }
99 //Make the objects visible again
100 if (oswap)
101 {
102 document.getElementById("hideObject").disabled = true;
103 }
104}
105
106detectFlash = function(version)
107{
108 if(navigator.plugins && navigator.plugins.length)
109 {
110 //Non-IE flash detection.
111 var plugin = navigator.plugins["Shockwave Flash"];
112 if (plugin == undefined)
113 {
114 return false;
115 }
116 var ver = navigator.plugins["Shockwave Flash"].description.split(" ")[2];
117 return (Number(ver) >= Number(version))
118 } else if (ie && typeof (ActiveXObject) == "function")
119 {
120 //IE flash detection.
121 try
122 {
123 var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + version);
124 return true;
125 }
126 catch(e)
127 {
128 return false;
129 }
130 }
131 //Catchall - skip detection
132 return true;
133}
134
135//Loop through an array of ids to strip
136//Replace the object by a div tag containing the same innerHTML.
137//To display an alternative image, message for the user or a link to the flash installation page, place it inside the object tag.
138//For the usual object/embed pairs it needs to be enclosed in comments to hide from gecko based browsers.
139stripFlash = function (stripQueue)
140{
141 if (!document.createElement)
142 {
143 return;
144 }
145 for (var i=0; i<stripQueue.length; i++)
146 {
147 var o = document.getElementById(stripQueue[i]);
148 var newHTML = o.innerHTML;
149 //Strip the comments
150 newHTML = newHTML.replace(/<!--\s/g, "");
151 newHTML =
作者: ulcer
- 该日志由 ulcer 于11年前发表在综合分类下,最后更新于 2013年07月02日.
- 转载请注明: javascript去除页面Flash虚线框 | 学步园 +复制链接
抱歉!评论已关闭.