JavaScript 日期和浏览器版本函数

发布于 2024-11-27 17:58:02 字数 1334 浏览 0 评论 0 原文

我似乎无法让这段代码工作。我试图做到这一点,如果浏览器是 IE6 或更低版本,它将使用“horarios2.png”作为 img src,而不是其他。如果是任何其他浏览器,它将检查工作日(0-6),以便填写每天的特定图像。从星期一(1)到星期三(3),图像应该是“quarta.png”。前几天有自己的形象。它们是分开工作的,但是当我尝试将它们放在一起时,我遇到了麻烦,并且它无法渲染图像。

脚本

$(document).ready (function horario () {
var date = new Date();
var weekday = (date.getDay());

function (msieversion) {
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )

if ( msie > 0 )
     return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
else
     return 0
}
if ((msieversion() == 0 )&&(weekday==0)) {
    document.getElementById('horarios').src = "img/domingo.png";}
else if ((msieversion() == 0 )&&(weekday==4)) {
    document.getElementById('horarios').src = "img/quinta.png";}
else if ((msieversion() == 0 )&&(weekday==5)) {
    document.getElementById('horarios').src = "img/sexta.png";}
else if ((msieversion() == 0 )&&(weekday==6)) {
    document.getElementById('horarios').src = "img/sabado.png";}
else if((msieversion() <= 6 )&&(weekday>=0)) {
    document.getElementById('horarios').src = "img/horarios2.png";}
else {
    document.getElementById('horarios').src = "img/quarta.png";}
});

HTML

<img id="horarios" border="0" alt="" width="343" height="45" />

I can't seem to get this code working. Im trying to make it so that if the browser is IE6 or lower it will use "horarios2.png" for the img src and no other. And if its any other browser that it will check the weekday(0-6) so that it will fill in the specific image for each day. From Monday(1) to Wensday(3) the image is suposed to be "quarta.png". The other day's have their own image. They work seperatly but when I try and put them together I get in trouble and it dosen't render the image.

Script

$(document).ready (function horario () {
var date = new Date();
var weekday = (date.getDay());

function (msieversion) {
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )

if ( msie > 0 )
     return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
else
     return 0
}
if ((msieversion() == 0 )&&(weekday==0)) {
    document.getElementById('horarios').src = "img/domingo.png";}
else if ((msieversion() == 0 )&&(weekday==4)) {
    document.getElementById('horarios').src = "img/quinta.png";}
else if ((msieversion() == 0 )&&(weekday==5)) {
    document.getElementById('horarios').src = "img/sexta.png";}
else if ((msieversion() == 0 )&&(weekday==6)) {
    document.getElementById('horarios').src = "img/sabado.png";}
else if((msieversion() <= 6 )&&(weekday>=0)) {
    document.getElementById('horarios').src = "img/horarios2.png";}
else {
    document.getElementById('horarios').src = "img/quarta.png";}
});

HTML

<img id="horarios" border="0" alt="" width="343" height="45" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦萦几度 2024-12-04 17:58:02

这就是我认为你的意思DEMO HERE(鼠标悬停在图像上)

function msieversion() {  
  var ua = window.navigator.userAgent
  var msie = ua.indexOf("MSIE ");
  return (msie == -1)?0: 
      parseInt(ua.substring (msie+5, ua.indexOf (".", msie )))
}
function getImg(idx) {
  return [
  "img/domingo.png",
  "img/quarta.png",
  "img/quarta.png",
  "img/quarta.png",
  "img/quinta.png",
  "img/sexta.png",
  "img/sabado.png"
  ][idx];
}
$(document).ready(function() {
  var weekday = new Date().getDay();
  if (msieversion() == 0 ) $("#horarios").attr("src",getImg(weekday));
  else $("#horarios").attr("src",(msieversion() <= 6 )? "img/horarios2.png":"img/quinta.png");
});

Here is what I think you meant DEMO HERE (mouseover the image)

function msieversion() {  
  var ua = window.navigator.userAgent
  var msie = ua.indexOf("MSIE ");
  return (msie == -1)?0: 
      parseInt(ua.substring (msie+5, ua.indexOf (".", msie )))
}
function getImg(idx) {
  return [
  "img/domingo.png",
  "img/quarta.png",
  "img/quarta.png",
  "img/quarta.png",
  "img/quinta.png",
  "img/sexta.png",
  "img/sabado.png"
  ][idx];
}
$(document).ready(function() {
  var weekday = new Date().getDay();
  if (msieversion() == 0 ) $("#horarios").attr("src",getImg(weekday));
  else $("#horarios").attr("src",(msieversion() <= 6 )? "img/horarios2.png":"img/quinta.png");
});
情绪 2024-12-04 17:58:02

您的代码可以进一步细化。

但在此之前,您可能需要使用“官方”方式来检测 IE 版本:
来自更有效地检测 Internet Explorer

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

现在,让我们研究一下逻辑:

  • 如果浏览器是 IE6 或更低版本,它将使用“horarios2.png”作为 img src,而不使用其他浏览器。
  • 如果是任何其他浏览器,它将检查工作日(0-6),以便填写每天的特定图像。
    • 从星期一(1)到星期三(3),图像应该是“quarta.png”。
    • 前几天有自己的形象

function horario ()  {
   var imageName = "";
   if(getInternetExplorerVersion() <= 6) {
      imageName = "img/horarios2.png";
      return;
   }

   var date = new Date();
   var weekday = (date.getDay());
   switch(weekday)
   {
      case 0:
          imageName = "img/domingo.png";
          break;
      case 1:
      case 2:
      case 3:
          imageName = "img/quarta.png";
          break;
      case 4:
          imageName = "img/quinta.png";
          break;
      case 5:
          imageName = "img/sexta.png";
          break;
      case 6:
          imageName = "img/sabado.png";
          break;
      default:
          imageName = "img/horarios2.png";
   }
   document.getElementById('horarios').src = imageName;
}

Your code can be refined more.

But before that, you may want to use the "official" way of detecting the version of IE:
(From Detecting Internet Explorer More Effectively)

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

Now, lets work on the logic:

  • If the browser is IE6 or lower it will use "horarios2.png" for the img src and no other.
  • And if its any other browser that it will check the weekday(0-6) so that it will fill in the specific image for each day.
    • From Monday(1) to Wensday(3) the image is suposed to be "quarta.png".
    • The other day's have their own image

:

function horario ()  {
   var imageName = "";
   if(getInternetExplorerVersion() <= 6) {
      imageName = "img/horarios2.png";
      return;
   }

   var date = new Date();
   var weekday = (date.getDay());
   switch(weekday)
   {
      case 0:
          imageName = "img/domingo.png";
          break;
      case 1:
      case 2:
      case 3:
          imageName = "img/quarta.png";
          break;
      case 4:
          imageName = "img/quinta.png";
          break;
      case 5:
          imageName = "img/sexta.png";
          break;
      case 6:
          imageName = "img/sabado.png";
          break;
      default:
          imageName = "img/horarios2.png";
   }
   document.getElementById('horarios').src = imageName;
}
白鸥掠海 2024-12-04 17:58:02

您可以从 html5boilerplate 获取一个页面(此示例不需要任何 html5 特定部分),在其中使用 元素的 className 中读取

>[if IE] 专有标记来检测 IE 版本,然后从 javascript href="http://jsfiddle.net/pxfunc/2qeHh/" rel="nofollow">jsfiddle 示例

HTML:

<!--[if lt IE 7]> <html lang="en-us" class="no-js ie6"> <![endif]--> 
<!--[if IE 7]>    <html lang="en-us" class="no-js ie7"> <![endif]--> 
<!--[if IE 8]>    <html lang="en-us" class="no-js ie8"> <![endif]--> 
<!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]--> 

JavaScript:

var htmlElement = document.getElementsByTagName('html')[0],
    horariosImg = document.getElementById('horarios'),
    imgSrc = '';

if (htmlElement.className.indexOf('ie6') > -1) {
    // do ie 6 (and lower) stuff
    imgSrc = 'img/horarios2.png';
} else {
    // do all other browser stuff
    switch ((new Date).getDay()) {
        case 0: // Sun
            imgSrc = 'img/domingo.png';
            break;
        case 1: // Mon
        case 2: // Tues
        case 3: // Wed
        case 4: // Thurs
            imgSrc = 'img/quinta.png';
            break;
        case 5: // Fri
            imgSrc = 'img/sexta.png';
            break;
        case 6: // Sat
            imgSrc = 'img/sabado.png';
            break;
    }
}
horariosImg.src = imgSrc;

You can take a page from the html5boilerplate (this example doesn't require any html5 specific parts) where you use the [if IE] proprietary markup to detect IE version, then read from the <html> element's className in javascript

jsfiddle example

HTML:

<!--[if lt IE 7]> <html lang="en-us" class="no-js ie6"> <![endif]--> 
<!--[if IE 7]>    <html lang="en-us" class="no-js ie7"> <![endif]--> 
<!--[if IE 8]>    <html lang="en-us" class="no-js ie8"> <![endif]--> 
<!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]--> 

JavaScript:

var htmlElement = document.getElementsByTagName('html')[0],
    horariosImg = document.getElementById('horarios'),
    imgSrc = '';

if (htmlElement.className.indexOf('ie6') > -1) {
    // do ie 6 (and lower) stuff
    imgSrc = 'img/horarios2.png';
} else {
    // do all other browser stuff
    switch ((new Date).getDay()) {
        case 0: // Sun
            imgSrc = 'img/domingo.png';
            break;
        case 1: // Mon
        case 2: // Tues
        case 3: // Wed
        case 4: // Thurs
            imgSrc = 'img/quinta.png';
            break;
        case 5: // Fri
            imgSrc = 'img/sexta.png';
            break;
        case 6: // Sat
            imgSrc = 'img/sabado.png';
            break;
    }
}
horariosImg.src = imgSrc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文