我有只想在某个日期出现的代码。这段代码在Firefox,Chrome甚至野生动物园中都可以正常工作。但是,该代码不适用于IE。而且我不知道为什么。
我发现IE中的.toLocaleString()用空格而不是逗号分隔它们。
function givingTuesdayCode(){
var isIE = /*@cc_on!@*/false || !!document.documentMode;
var now = calcTime(-6);
var splitnow = "";
if ( isIE ){ splitnow = now.split(" "); }
else{ splitnow = now.split(","); }
if (splitnow[0] == "12/2/2014"){
$('.introrotation').html("<img style='width:100%; height:auto' class='givingTuesday' src='/graphics/PoliticGov_620x265.jpg' alt='Give to us'/> <a href='http://IllinoisState.edu/GivingTuesday' class='GiveButtonLink'>Giving Tuesday: Join us!</a> <p style='color:black; position:relative; top:-85px; margin:10px'>Black Friday and Cyber Monday have come and gone. Today, join your fellow Redbirds and make a gift that matters. Give today at <a href='http://IllinoisState.edu/GivingTuesday' class='GiveLink'>IllinoisState.edu/GivingTuesday</a></p>");
$('.introrotation').css({'height': '265px'
});
$('.toggleButton').css({'display': 'none'
});
}
function calcTime(offset){
var date = new Date();
var utc = date.getTime()+(360*60000);
var nd = new Date(utc+(3600000*offset));
return nd.toLocaleString();
}
与其尝试将日期与特定字符串匹配(在其他语言环境中可能会中断),不如直接将特定日期进行比较:
// Reset the hours, minutes, etc. so that comparison works
var today = (new Date()).setHours(0, 0, 0, 0);
// Month is zero-indexed (i.e. 0 = Jan, 11 = Dec)
var specificDate = (new Date(2014, 11, 2)).getTime();
if (today === specificDate) {
$('.introrotation').html("<img style='width:100%; height:auto' class='givingTuesday' src='/graphics/PoliticGov_620x265.jpg' alt='Give to us'/> <a href='http://IllinoisState.edu/GivingTuesday' class='GiveButtonLink'>Giving Tuesday: Join us!</a> <p style='color:black; position:relative; top:-85px; margin:10px'>Black Friday and Cyber Monday have come and gone. Today, join your fellow Redbirds and make a gift that matters. Give today at <a href='http://IllinoisState.edu/GivingTuesday' class='GiveLink'>IllinoisState.edu/GivingTuesday</a></p>");
$('.introrotation').css({'height': '265px'});
$('.toggleButton').css({'display': 'none'});
}
有关比较日期的更多信息,请参见使用JavaScript比较两个日期。
理想情况下,您应该在服务器端而不是客户端上执行此操作,以便对于禁用JavaScript的浏览器仍然显示您的消息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句