<script type="text/javascript"> var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0; //以下进行测试 if (Sys.ie) document.write('IE: ' + Sys.ie); if (Sys.firefox) document.write('Firefox: ' + Sys.firefox); if (Sys.chrome) document.write('Chrome: ' + Sys.chrome); if (Sys.opera) document.write('Opera: ' + Sys.opera); if (Sys.safari) document.write('Safari: ' + Sys.safari); </script>
jquery版
<script src="https://www.jb51.net/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ var bro=$.browser; var binfo=""; if(bro.msie) {binfo="Microsoft Internet Explorer "+bro.version;} if(bro.mozilla) {binfo="Mozilla Firefox "+bro.version;} if(bro.safari) {binfo="Apple Safari "+bro.version;} if(bro.opera) {binfo="Opera "+bro.version;} alert(binfo); }) </script>
19、判断是否开启cookie
<script> function checkCookie() { var result=false; if(navigator.cookiesEnabled){ return true; } document.cookie = "testcookie=yes;"; var setCookie = document.cookie; if (setCookie.indexOf("testcookie=yes") > -1){ result=true; }else{ document.cookie = ""; } return result; } if(!checkCookie()){ alert("对不起,您的浏览器的Cookie功能被禁用,请开启"); }else{ alert("Cookie 成功开启"); } </script>
20、 断是否开启JavaScript
// 方案 1 <span>您关闭了JavaScript</span> <script type="text/javascript"> <!-- document.getElementById("js_enable").innerHTML='您开启了JavaScript'; --> </script> // 方案 2 <div >您禁用了javascript。</div> <div>您的Javascript是开启的</div> <script> var NoJs= document.getElementById("NoJs"); var YesJs= document.getElementById("YesJs"); NoJs.style.display="none"; YesJs.style.display="block"; </script> // 方案 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>检查浏览器是否开启JavaScript</title> </head> <body> 如果您的浏览器支持的话,本页面什么也不会显示,如果不支持,则会出现提示! <noscript> <body scroll=no> <center> <table> <tr> <td> <div> <span>对不起,你的浏览器没有打开JavaScript脚本支持!</span></div> </td> </tr> </table> </center> </noscript> </body> </html>
HTML <noscript> 标签
定义和用法 noscript 元素用来定义在脚本未被执行时的替代内容(文本)。
此标签可被用于可识别 <script> 标签但无法支持其中的脚本的浏览器。
注释:如果浏览器支持脚本,那么它不会显示出 noscript 元素中的文本。
注释:无法识别 <script> 标签的浏览器会把标签的内容显示到页面上。为了避免浏览器这样做,您应当在注释标签中隐藏脚本。老式的(无法识别 <script> 标签的)浏览器会忽略注释,这样就不会把标签的内容写到页面上,而新式的浏览器则懂得执行这些脚本,即使它们被包围在注释标签中!
21、JavaScript 打字机效果
实例1
<html> <title>JavaScript 打字机</title> <head> <style type="text/css"> body{ font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif; margin-top:0px; background-image:url('../../images/heading3.gif'); background-repeat:no-repeat; padding-top:100px; } #myContent, #myContent blink{ width:500px; height:200px; background:black; color: #00FF00; font-family:courier; } blink{ display:inline; } </style> <script type="text/javascript"> var charIndex = -1; var stringLength = 0; var inputText; function writeContent(init){ if(init){ inputText = document.getElementById('contentToWrite').innerHTML; } if(charIndex==-1){ charIndex = 0; stringLength = inputText.length; } var initString = document.getElementById('myContent').innerHTML; initStringinitString = initString.replace(/<SPAN.*$/gi,""); var theChar = inputText.charAt(charIndex); var nextFourChars = inputText.substr(charIndex,4); if(nextFourChars=='<BR>' || nextFourChars=='<br>'){ theChar = '<BR>'; charIndex+=3; } initStringinitString = initString + theChar + "<SPAN>_</SPAN>"; document.getElementById('myContent').innerHTML = initString; charIndexcharIndex = charIndex/1 +1; if(charIndex%2==1){ document.getElementById('blink').style.display='none'; }else{ document.getElementById('blink').style.display='inline'; } if(charIndex<=stringLength){ setTimeout('writeContent(false)',150); }else{ blinkSpan(); } } var currentStyle = 'inline'; function blinkSpan(){ if(currentStyle=='inline'){ currentStyle='none'; }else{ currentStyle='inline'; } document.getElementById('blink').style.display = currentStyle; setTimeout('blinkSpan()',500); } </script> <body> <div> </div> <div> <!-- Put the typewriter content here--> sharejs.com Login : username<br> password : ******<br> Access is granted<br> <!-- End typewriter content --> </div> <script type="text/javascript"> writeContent(true); </script> </body> </html>
实例2 (兼容IE,FX)