Javascript 日期时间比较返回 false,但分支 stmt 仍然执行
Javascript date time comparison returns false but branch stmt still executed
我对这种Javascript行为感到困惑。检查此代码。
var NoOfMonthsElapsed = 6; //Should be >= 1 and <= 12
var MsgURL = "about:blank";
var PopupTitle = "ContactInfoUpdate";
var OptionString = "height=165,width=400,menubar=0,toolbar=0,location=1,status=0,resizable=0,status=0,HAlign=center,top=300";
var lastUpdatedDate = crmForm.all.dxb_lastcontactinfoupdatedon.DataValue; //Reads a field with date value = 01 Jan 2010
if (lastUpdatedDate)
{
var month = lastUpdatedDate.getMonth();
var year = lastUpdatedDate.getYear();
var date = lastUpdatedDate.getDate();
month = month + NoOfMonthsElapsed;
year = year + parseInt(month / 11);
month = (month % 11);
var today = new Date();
var showPopupAfterDate = new Date();
showPopupAfterDate.setYear(year);
showPopupAfterDate.setMonth(month);
var alertMsg = "LastUpdatedDate = "+ lastUpdatedDate + "'n"
var alertMsg += "Today = "+ today + "'n"
var alertMsg += "PopupAfterDate = "+ showPopupAfterDate + "'n"
var alertMsg += "Today>showPopupAfterDate = "+ (today>showPopupAfterDate) + "'n"
alert(alertMsg);
if (today>showPopupAfterDate);
{
window.open(MsgURL, PopupTitle, OptionString);
}
}
else
{
window.open(MsgURL, PopupTitle, OptionString);
}
//
// It displays the following output
//
LastUpdatedDate = Wed May 18 20:56:00 UTC+0400 2011
Today = Fri May 18 20:23:49 UTC+0400 2011
PopupAfterDate = Fri Nov 18 20:23:49 UTC+0400 2011
Today>showPopupAfterDate = false
为什么今天显示为星期五五月18 2011... 虽然五月18 2011是星期三为什么弹出窗口日期显示为星期五十一月18 2011...即使日期比较返回假;窗口打开仍然被执行。
你的你在错误的地方有{
和}
很乱,;
。
if (lastUpdatedDate) {
....
if (today>showPopupAfterDate) { // notice I removed ;
window.open(MsgURL, PopupTitle, OptionString);
}
} else {
window.open(MsgURL, PopupTitle, OptionString);
}
尾随分号关闭if
语句:
if (today>showPopupAfterDate);
// --------------------------^
发现问题:
if (today>showPopupAfterDate) //<-- remove the `;`
{
window.open(MsgURL, PopupTitle, OptionString);
}
你的代码正在运行 if,停止,然后执行下一个语句,即window.open
相关文章:
- 在angularjs/google地图中显示分支最近的路线
- 在子菜单页面上时,mmenu打开子菜单分支
- 如何更改分支智能横幅的位置
- 如何将分支用于 yeoman 生成器
- JavaScript,if 语句总是只执行 'else' 分支
- 社区是否有任何将要维护的 KinecticJS 分支,我们可以依赖它
- 切换树列表 Backone 和木偶中的分支
- 在流星中的 3 个子分支中进行反应性连接
- 谷歌浏览器扩展程序:如何使用层次结构分支创建右键单击扩展程序
- 如何在循环中测试就绪状态和状态以避免 else 分支
- 为什么 JavaScript 警报不会在 ASP 经典的 Select Case 分支中弹出
- 什么是JavaScript单元测试的代码覆盖率的分支
- NodeGit:在不签出的情况下将分支指针移动到不同的提交
- 引用JSON树结构的分支的最佳方法是什么
- jQueryAJAX-对结果进行分支
- 用于分析GitHub分支引用的Regex
- 用于将树分支从集合移动到另一个集合的Firebase API
- 处理条if/else同时呈现两个分支
- jQuery选择一个类的所有节点,但不选择当前DOM分支中的节点
- Javascript 日期时间比较返回 false,但分支 stmt 仍然执行