Javascript在chrome中工作,而不是firefox或ie

时间:2015-11-05 02:09:22

标签: javascript jquery

我是javascript / jquery的新手。编写了以下脚本,它适用于Chrome 46.0.2490.80 m,但不适用于Firefox 42.0或IE 11。 格式为%<value_name>%的值是我正在使用的内容管理系统的预定义值。

如果布尔值"%asset_metadata_register_button%"设置为0,或者在日期变量"eventDate"发生一天后,脚本应该隐藏按钮。否则显示按钮。

该脚本还应该在href发生后更改按钮的文本和eventDate值。

HTML

<p id="show"><a id="change" class="button" href="firstURl">Register</a></p>

的Javascript

$(document).ready(function() {
    // Boolean value from Show Registration Button metadata field
    var number = "%asset_metadata_register_button%";

    // variable for start date of event
    var eventDate = new Date('%asset_attribute_start_date%');

    // variable for date at present time
    var now = new Date();

    // variable for one day after present time
    var oneDay = new Date('%asset_attribute_start_date%');

    oneDay.setDate(oneDay.getDate() + 1);

    // Hide button if Show Registration Button metadata field is set to no, or for one day after Webinar took place. Otherwise show Button.
    if (number == 1) {
        if (eventDate < now && now < oneDay) {
            document.getElementById("show").innerHTML = "(Webinar will be uploaded shortly)";
        } else {
            $('#show').show();
        }
    } else {
        $('#show').hide();
    }

    // changes button text and link after webinar commences
    if (eventDate > now) {
        document.getElementById("change").innerHTML = "View Webinar";
        document.getElementById("change").href = "secondURL";
    }
});

1 个答案:

答案 0 :(得分:0)

正如+ Jaromanda X所述,某些浏览器出现错误。

您的代码如下所示:

// variable for start date of event
var eventDate = new Date('%asset_attribute_start_date%');

// variable for date at present time
var now = new Date();

//variable for one day after present time
var oneDay = new Date('%asset_attribute_start_date%');

oneDay.setDate(oneDay.getDate() + 1);

您可以将其替换为:

var dateTime = '%asset_attribute_start_date%';
var date = dateTime.substring(0, dateTime.indexOf(" "));
var time = dateTime.substring(dateTime.indexOf(" ")+1);

date = date.substring(date.indexOf("-")+1) + "-" + date.substring(0, date.indexOf("-"));

var eventDate = new Date(date + ' ' + time);
var oneDay = new Date();
oneDay.setDate(eventDate.getDate() + 1);

var now = new Date();

它的作用是替换“2015-11-04”中的日期格式。到&#39; 11-04-2015&#39;。