Rumah  >  Artikel  >  hujung hadapan web  >  Javascript自定义函数判断网站访问类型是PC还是移动终端

Javascript自定义函数判断网站访问类型是PC还是移动终端

高洛峰
高洛峰asal
2017-01-23 17:30:261094semak imbas

由于很多移动终端不支持 Flash,因此 许多绚丽多彩的 Flash效果出不来。如果,能够判断出访问Web网页的类型(PC还是移动终端)。就可以对症下药,找出解决的办法! 

访问的类型为移动终端我们就用.gif代替Flash(.swf后缀)动画,PC端就不做改变。这样就比较完美了! 

如下所示,函数 flashChecker() 就是用来检测访问的类型。 

<script language="javascript" type="text/javascript"> 
/* 
*用来检测是 PC还是移动终端 
*返回:flashChecker().f == true PC终端 
* 反之为移动终端 
*/ 
function flashChecker() { 
var hasFlash = 0; 
var flashVersion = 0; 
var isIE = /*@cc_on!@*/0; 
if (isIE) { 
var swf = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"); 
if (swf) { 
hasFlash = 1; 
VSwf = swf.GetVariable("$version"); 
flashVersion = parseInt(VSwf.split(" ")[1].split(",")[0]); 
} 
} 
else { 
if (navigator.plugins && navigator.plugins.length > 0) { 
var swf = navigator.plugins["Shockwave Flash"]; 
if (swf) { 
hasFlash = 1; 
var words = swf.description.split(" "); 
for (var i = 0; i < words.length; ++i) { 
if (isNaN(parseInt(words[i]))) { 
continue; 
} 
flashVersion = parseInt(words[i]); 
} 
} 
} 
} 
return { 
f: hasFlash, v: flashVersion 
}; 
} 
</script>

扩充: 

<script language="javascript" type="text/javascript"> 
/* 
* 根据参数 输出swf动画 
* url :单击 swf动画 后的跳转地址 
* swfLink: *.swf 素材地址 
*/ 
function GetSwfHtml(url, swfLink) { 
html = "<a style=&#39;position: absolute; top: 0; left: 0; bottom: 0; right: 0; display: block; " + 
"width: 100%; height: expression(this.parentNode.scrollHeight); filter: alpha(opacity=0);" + 
"opacity: 0; background: #FFF;&#39; href=&#39;" + url + "&#39; target=&#39;_blank&#39;>" + 
"</a>" + 
"<object width=&#39;590&#39; height=&#39;55&#39; align=&#39;middle&#39;>" + 
"<param name=&#39;allowScriptAccess&#39; value=&#39;never&#39; />" + 
"<param name=&#39;quality&#39; value=&#39;high&#39; />" + 
"<param name=&#39;wmode&#39; value=&#39;transparent&#39; />" + 
"<param name=&#39;movie&#39; value=&#39;" + swfLink+ "&#39; />" + 
"<embed wmode=&#39;transparent&#39; src=&#39;" + swfLink+ "&#39; quality=&#39;high&#39; " + 
"width=&#39;590&#39; height=&#39;55&#39; align=&#39;middle&#39; allowscriptaccess=&#39;never&#39; type=&#39;application/x-shockwave-flash&#39; " + 
"pluginspage=&#39;" + url + "&#39; />" + 
"</object>"; 


return html; 
} 
</script>

更多Javascript自定义函数判断网站访问类型是PC还是移动终端相关文章请关注PHP中文网!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn